Defensive programming: Difference between revisions

Content deleted Content added
Secure programming: ^Missed some
Tag: Reverted
Reverted good faith edits by 2601:1C0:D:3E46:A02A:2259:E628:EFF (talk): Sizeof in fact does work on arrays (and is required to not perform an implicit array-to-pointer conversion). Furthermore, with your changes the code would potentially apply strlen on a character array without NUL termination.
Tags: Twinkle Undo Reverted
Line 37:
strncpy(str, input, strlen(str));
 
// If strlen(input) >= strlensizeof(str) then strncpy won't null terminate.
// We counter this by always setting the last character in the buffer to NUL,
// effectively cropping the string to the maximum length we can handle.
// One can also decide to explicitly abort the program if strlen(input) is
// too long.
str[strlensizeof(str) - 1] = '\0';
 
// ...