AP_ROMFS: fix buffer null terminator

Ensure buffer is properly null terminated without changing the indicated
size even for uncompressed data.
This commit is contained in:
Thomas Watson 2024-01-24 12:16:01 -06:00 committed by Andrew Tridgell
parent d13193150c
commit 52468f0238
2 changed files with 13 additions and 9 deletions

View File

@ -32,17 +32,21 @@ def embed_file(out, f, idx, embedded_name, uncompressed):
write_encode(out, '__EXTFLASHFUNC__ static const uint8_t ap_romfs_%u[] = {' % idx) write_encode(out, '__EXTFLASHFUNC__ static const uint8_t ap_romfs_%u[] = {' % idx)
if uncompressed: if uncompressed:
# ensure nul termination # terminate if there's not already an existing null. we don't add it to
if contents[-1] != 0: # the contents to avoid storing the wrong length
contents += bytes([0]) null_terminate = 0 not in contents
b = contents b = contents
else: else:
# compress it (max level, max window size, raw stream, max mem usage) # compress it (max level, max window size, raw stream, max mem usage)
z = zlib.compressobj(level=9, method=zlib.DEFLATED, wbits=-15, memLevel=9) z = zlib.compressobj(level=9, method=zlib.DEFLATED, wbits=-15, memLevel=9)
b = z.compress(contents) b = z.compress(contents)
b += z.flush() b += z.flush()
# decompressed data will be null terminated at runtime, nothing to do here
null_terminate = False
write_encode(out, ",".join(str(c) for c in b)) write_encode(out, ",".join(str(c) for c in b))
if null_terminate:
write_encode(out, ",0")
write_encode(out, '};\n\n'); write_encode(out, '};\n\n');
return crc, len(contents) return crc, len(contents)

View File

@ -45,10 +45,10 @@ const AP_ROMFS::embedded_file *AP_ROMFS::find_file(const char *name)
} }
/* /*
find a compressed file and uncompress it. Space for decompressed find a compressed file and uncompress it. Space for decompressed data comes
data comes from malloc. Caller must be careful to free the resulting from malloc. Caller must be careful to free the resulting data after use. The
data after use. The next byte after the file data is guaranteed to file data buffer is guaranteed to contain at least one null (though it may be
be null at buf[size]).
*/ */
const uint8_t *AP_ROMFS::find_decompress(const char *name, uint32_t &size) const uint8_t *AP_ROMFS::find_decompress(const char *name, uint32_t &size)
{ {
@ -58,7 +58,7 @@ const uint8_t *AP_ROMFS::find_decompress(const char *name, uint32_t &size)
} }
#ifdef HAL_ROMFS_UNCOMPRESSED #ifdef HAL_ROMFS_UNCOMPRESSED
size = f->compressed_size; size = f->decompressed_size;
return f->contents; return f->contents;
#else #else
uint8_t *decompressed_data = (uint8_t *)malloc(f->decompressed_size+1); uint8_t *decompressed_data = (uint8_t *)malloc(f->decompressed_size+1);
@ -66,7 +66,7 @@ const uint8_t *AP_ROMFS::find_decompress(const char *name, uint32_t &size)
return nullptr; return nullptr;
} }
// explicitly null terimnate the data // explicitly null-terminate the data
decompressed_data[f->decompressed_size] = 0; decompressed_data[f->decompressed_size] = 0;
TINF_DATA *d = (TINF_DATA *)malloc(sizeof(TINF_DATA)); TINF_DATA *d = (TINF_DATA *)malloc(sizeof(TINF_DATA));