Messages in this thread | | | Subject | Re: [PATCH v3 2/2] lib/test-string_helpers.c: add string_get_size() tests | From | Andy Shevchenko <> | Date | Tue, 15 Sep 2015 15:19:18 +0300 |
| |
On Tue, 2015-09-15 at 14:10 +0200, Vitaly Kuznetsov wrote: > Andy Shevchenko <andriy.shevchenko@linux.intel.com> writes: > > > I prefer to put '\0' at the position after we expected have an > > actual > > '\0'. In this case we always be NULL terminated. I did this for > > hexdump > > test cases. > > Just to check I got your suggestions right: > > ... > + if (!memcmp(buf, exp_result, strnlen(exp_result, sizeof(buf) > - 1) + 1)) > + return; > + > + /* NULL terminate buf right after the expected '\0' */ > + buf[strnlen(exp_result, sizeof(buf) - 2) + 1] = '\0'; > ... > > Alternatively, we could have avoided strnlen() by asserting > strlen(exp_result) < sizeof(buf) - 1 at the very beginning. >
Just
buf[sizeof(buf) - 1] = '\0';
should be enough after you called the string_get_size().
And minimize the buffer to something like 16 (whatever is the biggest possible length + '\0' + some space for the wrong algorithm aligned to let's say 4.
-- Andy Shevchenko <andriy.shevchenko@linux.intel.com> Intel Finland Oy
| |