Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
bgColor#FFCCCC
enum { NO_FILE_POS_VALUES = 3 };

int opener(
  FILE *file, 
  int *width, 
  int *height, 
  int *data_offset
) {
  int file_w;
  int file_h;
  int file_o;
  fpos_t offset;

  if (file == NULL) { return EINVAL; }
  errno = 0;
  if (fgetpos(file, &offset) != 0) { return errno; }
  if (fscanf(file, ""%i %i %i"", &file_w, &file_h, &file_o) 
        != NO_FILE_POS_VALUES) { 
    return EIO; 
  }

  errno = 0;
  if (fsetpos(file, &offset) != 0) { return errno; }

  if (width != NULL) { *width = file_w; }
  if (height != NULL) { *height = file_h; }
  if (data_offset != NULL) { *data_offset = file_o; }

  return 0;
}

...

Code Block
bgColor#ccccff
#include <errno.h>&lt;errno.h&gt;

enum { NO_FILE_POS_VALUES = 3 };

errno_t opener(
  FILE *file, 
  int *width, 
  int *height, 
  int *data_offset
) {
  int file_w;
  int file_h;
  int file_o;
  fpos_t offset;

  if (file == NULL) { return EINVAL; }
  errno = 0;
  if (fgetpos(file, &amp;offset) != 0 ) { return errno; }
  if (fscanf(file, "&quot;%i %i %i"&quot;, &amp;file_w, &amp;file_h, &amp;file_o) 
        != NO_FILE_POS_VALUES) { 
    return EIO; 
  }

  errno = 0;
  if (fsetpos(file, &amp;offset) != 0 ) { return errno; }

  if (width != NULL) { *width = file_w; }
  if (height != NULL) { *height = file_h; }
  if (data_offset != NULL) { *data_offset = file_o; }

  return 0;
}

...

Search for vulnerabilities resulting from the violation of this rule on the CERT website.

Other Languages

This rule appears in the C++ Secure Coding Standard as DCL09-CPP. Declare functions that return an errno error code with a return type of errno_t.

References

Wiki Markup
\[[ISO/IEC 9899:1999|AA. C References#ISO/IEC 9899-1999]\] Section 6.7.5.3, "&quot;Function declarators (including prototypes)"&quot;
\[[ISO/IEC PDTR 24772|AA. C References#ISO/IEC PDTR 24772]\] "&quot;NZN Returning error status"&quot;
\[[ISO/IEC TR 24731-1:2007|AA. C References#ISO/IEC TR 24731-1-2007]\]
\[[MISRA 04|AA. C References#MISRA 04]\] Rule 20.5
\[[Open Group 04|AA. C References#Open Group 04]\]

...

      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;02. Declarations and Initialization (DCL)       &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;