Closed SafeCoding233 closed 1 month ago
As an application (not a library) that runs only on Linux systems, whether it is necessary to check the return value of malloc
for NULL
is a debatable issue due to the default overcommit handling mode and the intervention of the OOM killer. See When malloc() Never Returns NULL -- Reliability as an Illusion for more.
I don't mind adding this check, as malloc
calls can still return NULL
if the default overcommit mode kernel configuration is modified, checking the return value still makes sense sometimes.
But changing perror("strdup failed to allocate memory");
to perror("strdup");
is more concise and clear, as perror
will automatically print the corresponding error message.
Thank you for your reference and suggestions! I modified the error messages.
The library function
strdup
may return null pointers, so I added some checks.