hmgle / graftcp

A flexible tool for redirecting a given program's TCP traffic to SOCKS5 or HTTP proxy.
GNU General Public License v3.0
2.1k stars 173 forks source link

Check null pointer for strdup #72

Closed SafeCoding233 closed 1 month ago

SafeCoding233 commented 1 month ago

The library function strdup may return null pointers, so I added some checks.

hmgle commented 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.

SafeCoding233 commented 1 month ago

Thank you for your reference and suggestions! I modified the error messages.