News for hackers. Stuff that matters.
Re:OpenBSD, C, httpd and SQLite – Web App stack by IshKebab on Saturday June 09, 2018 @06:59AM (#17272423)
Yes but they have well defined and well tested places where that happens.

For example you could implement a string class in C++ using strlen, strdup etc. if you were feeling insane and it would probably be fine since you only use strdup once and then all the users of you class don't have to worry about getting it wrong.

If you write in C you have to use strdup every time you copy a string and there's no way you get it right 1000 times.

Re:OpenBSD, C, httpd and SQLite – Web App stack by jstewartmobile on Saturday June 09, 2018 @10:35AM (#17273226)
The phrase "have to" here is VM language Stockholm syndrome.

You'd be surprised how far large static buffers and snprintf will get you. Easy to deal with, no leaks, valgrind is happy...

Re:OpenBSD, C, httpd and SQLite – Web App stack by tedunangst on Saturday June 09, 2018 @07:01AM (#17272434)
It's pretty hard to get strdup wrong.
Re:OpenBSD, C, httpd and SQLite – Web App stack by sunfish on Saturday June 09, 2018 @07:24AM (#17272491)
Re:OpenBSD, C, httpd and SQLite – Web App stack by tedunangst on Saturday June 09, 2018 @08:32AM (#17272704)
This is true. Although they did say "copy a string", not use strdup to copy a thing that is not a string. :)
Re:OpenBSD, C, httpd and SQLite – Web App stack by sunfish on Saturday June 09, 2018 @10:28AM (#17273182)
Yes, that's one of the three CVE's I posted ;).

You got me thinking about ways one could get strdup wrong:

- input is not a string -> possible UB

- input is a string, but the character encoding wasn't what you thought -> possible UB

- input is a string, but it was the pointer-plus-length kind -> possible UB

- input is modified by another thread -> possible UB

- strdup called from within a signal handler -> possible UB

- failure to handle error return values -> possible UB

- failure to free the memory when it's no longer needed -> memory leak

- freed the memory more than once -> possible UB

- used the memory after freeing it -> possible UB

I've personally seen several of these in real-world code.

Re:OpenBSD, C, httpd and SQLite – Web App stack by sunfish on Saturday June 09, 2018 @01:01PM (#17273869)
How did I forget:

- forget to #include string.h, so strdup is implicitly declared, so its return type is int, which implicitly converts to char*, so everything still compiles -> possible UB