sheredom / utest.h

🧪 single header unit testing framework for C and C++
The Unlicense
834 stars 57 forks source link

Fix logic for handling test case name allocation. #148

Closed ericonr closed 7 months ago

ericonr commented 7 months ago

Setting the test case information and writing into its name should only happen when both allocations succeed; otherwise, it might be necessary to free one or both of the used pointers. utest_state.tests has to be set to NULL to avoid a UAF as well.


In case support for free(NULL); is a reasonable assumption, the code can be shortened a bit by avoiding the unnecessary conditionals. Please let me know :)

One curiosity that came up while I was looking at this and #147 is: what's the intended behavior in case memory allocation fails while instantiating test cases? I didn't take a deep look, but it seemed to me like some tests would simply be omitted, without warning the user.

ericonr commented 7 months ago

Yeah I think at present we'd just do what we had. Maybe we should make it fail nicely on startup if you have an allocation problem.

I feel like there could be a utest_malloc, and both it and utest_realloc should print something to stderr and then call exit(1);.

Does that seem reasonable?

sheredom commented 7 months ago

Yeah that seems reasonable!