Fix ancient off-by-one at end boundary in string array size calculation
authorPanu Matilainen <pmatilai@redhat.com>
Fri, 21 Oct 2011 08:49:53 +0000 (11:49 +0300)
committerPanu Matilainen <pmatilai@redhat.com>
Fri, 21 Oct 2011 10:33:59 +0000 (13:33 +0300)
commitf79909d04e43cbfbbcdc588530a8c8033c5e0a7c
tree5ff149d0b73e792673b696cde3d2e952d2d0f827
parentbfcc5bf3b9bfe77e7771eb947b17ffea87192ee7
Fix ancient off-by-one at end boundary in string array size calculation

- String array size calculation could read one byte past data end
  pointer when expected count and number of \0's disagree (ie invalid data)
  due to while condition side-effects + bounds checking being in
  the inner loop.
- Lift the string length calculation to inline helper function, used for
  both string and string array types.
- Streamline the calculations:
  - Eliminate unnecessary length increments, calculate the length
    from pointer distance
  - Eliminate end pointer NULL checking within the loop: when caller
    doesn't supply end pointer, cap to HEADER_MAX_DATA (ie 16MB),
    anything larger would trip up in later hdrchkData() checks anyway.
  - Avoid the off-by-one by eliminating the problematic inner loop.
lib/header.c
lib/header_internal.h