Rewrite most of scrub_for_print() to safely handle corner cases (negative return value from snprintf(), output buffer too small).
Changes from v1 of this patch series: * Add a comment saying that 'used' is equal to the untruncated string length until the output becomes truncated, at which point it equals the output buffer size (to make it clear that the 'used+=' line doesn't have an off-by-one bug). * Add tests that check whether scrub_for_print() overruns the output buffer. These tests fail in the commits before the bugs are fixed and pass afterward. * Reorder a couple of commits: Add the output length tests before changing the documentation to make it clear that the tests are for the intended behavior, not the behavior as documented in the documentation change commit. * Reword commits: - "add tests for scrub_for_print()'s output length": spell out that the tests assume that the output length is the untruncated string length, and give justification - "document scrub_for_print() as it currently behaves": make it clear that I'm changing the documentation to match the current behavior, not the intended behavior, and that future commits will modify the function to go back to the intended behavior Richard Hansen (15): whitespace fixes add missing #include guard in lib/util/stringutils.h include stringutils.h file before other headers add missing #include move function documentation to function declaration test for buffer overruns in testing scrub_for_print() use random bytes when testing for buffer overruns add tests for scrub_for_print()'s output length document scrub_for_print() as it currently behaves factor out nearly-identical snprintf() calls in scrub_for_print() allow NULL for scrub_for_print()'s other_chars_to_escape param fix scrub_for_print()'s for loop condition fix scrub_for_print() when output buffer is too small fix scrub_for_print() when snprintf() fails allow dst==NULL and dst_sz==0 for scrub_for_print() lib/util/stringutils.c | 149 +++++++++----------------------------- lib/util/stringutils.h | 126 +++++++++++++++++++++++++++++++- lib/util/tests/stringutils-test.c | 77 +++++++++++++++++--- 3 files changed, 228 insertions(+), 124 deletions(-) -- 2.4.3 ------------------------------------------------------------------------------ _______________________________________________ rpstir-devel mailing list rpstir-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/rpstir-devel