...
Code Block |
---|
|
...
size_t size = strlen(input_str);
if (size == SIZE_MAX) { /* test for limit of size_t */
/* Handle Error */
}
str = malloc(size+1);
strcpy(str, input_str);
...
|
Note that in accordance with rule MEM35-C. Ensure that size arguments to memory allocation functions are valid the argument supplied to malloc()
is checked to ensure a numeric overflow does not occur.
Compliant Solution
Wiki Markup |
---|
To correct this error, ensure the pointer returned by {{malloc()}} is not invalid (null). In addition to this rule, this should be done in accordance with rule \[[MEM32-C|MEM32-C. Detect and handle critical memory allocation errors]\]. |
Code Block |
---|
|
...
size_t size = strlen(input_str);
if (size == SIZE_MAX) { /* test for limit of size_t */
/* Handle Error */
}
str = malloc(size+1);
if (str == NULL) {
/* Handle Allocation Error */
}
strcpy(str, input_str);
...
|
...