simon
simon

Reputation: 1234

Linux module __must_check annotation

I am learning about Linux kernel module development. I read articles and tutorials, and I found a site which supplies source code for a simple char device.

In the code a __must_check is used for one function.

Here is the prototype:

__must_check int register_device(void);

This is the function:

int register_device(void)
{
  int result = 0;

  printk( KERN_NOTICE "Simple-driver: register_device() is called." );

  result = register_chrdev( 0, device_name, &simple_driver_fops );
  if( result < 0 )
  {
     printk( KERN_WARNING "Simple-driver:  can\'t register character device with errorcode = %i", result );
     return result;
  }

  device_file_major_number = result;
  printk( KERN_NOTICE "Simple-driver: registered character device with major number = %i and minor numbers 0...255"
              , device_file_major_number );

  return 0;
}

What is the utility of __must_check? This is the only code I found that used this.

Upvotes: 4

Views: 3830

Answers (1)

Danh
Danh

Reputation: 6016

__must_check is defined as:

#define __must_check __attribute__((warn_unused_result))

Quotes from Common Function Attributes

The warn_unused_result attribute causes a warning to be emitted if a caller of the function with this attribute does not use its return value. This is useful for functions where not checking the result is either a security problem or always a bug, such as realloc.

This is also applied to clang and Intel compiler.

This macro asks compiler to issue a warning if the return value is not used. This is important with function return value to indicate success or failure like scanf, printf, or function return memory like malloc, realloc.

Upvotes: 9

Related Questions