Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Checking for null strings

...

Code Block
bgColor#FFCCCC
langc
#include <stdlib.h>
#include <string.h>
 
typedef struct {
  char sig_desc[32];
} signal_info;
 
void func(size_t num_of_records, size_t temp_num,
          const char *tmp2) {
  signal_info *start = (signal_info *)malloc(num_of_records *
                                          sizeof(signal_info));
  signal_info *point = start + temp_num - 1;
  if (tmp2 == NULL) {
    /* Handle error */
  } 
  memcpy(point->sig_desc, tmp2, strlen(tmp2));
  /* ... */ 
}

...

Code Block
bgColor#ccccff
langc
#include <stdlib.h>
#include <string.h>
 
typedef struct {
  char sig_desc[32];
} signal_info;
 
void func(size_t num_of_records, size_t temp_num,
          const char *tmp2) {
  signal_info *point;
  signal_info *start = (signal_info *)malloc(num_of_records *
                                           sizeof(signal_info));
  if (start == NULL) {
    /* Handle allocation error */
  } else if (tmp2 == NULL) {
    /* Handle error */
  } 
  point = start + temp_num - 1; 
  memcpy(point->sig_desc, tmp2, strlen(tmp2));
  /* ... */ 
}

...

[DHS 2006]Handle All Errors Safely
[Henricson 1997]Recommendation 12.1, "Check for All Errors Reported from Functions"
[ISO/IEC 9899:2011]Subclause 7.21.7.10, "The ungetc Function"