AP_ROMFS: clarify usage and null termination

Also remove the redundant insertion of the null terminator.
This commit is contained in:
Thomas Watson 2024-02-23 15:59:40 -06:00 committed by Andrew Tridgell
parent 2a218221f0
commit a5764b7413
2 changed files with 11 additions and 14 deletions

View File

@ -45,10 +45,9 @@ const AP_ROMFS::embedded_file *AP_ROMFS::find_file(const char *name)
} }
/* /*
find a compressed file and uncompress it. Space for decompressed data comes Find the named file and return its decompressed data and size. Caller must
from malloc. Caller must be careful to free the resulting data after use. The call AP_ROMFS::free() on the return value after use to free it. The data is
file data buffer is guaranteed to contain at least one null (though it may be guaranteed to be null-terminated such that it can be treated as a string.
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)
{ {
@ -61,20 +60,18 @@ const uint8_t *AP_ROMFS::find_decompress(const char *name, uint32_t &size)
size = f->decompressed_size; size = f->decompressed_size;
return f->contents; return f->contents;
#else #else
// add one byte for null termination; ArduPilot's malloc will zero it.
uint8_t *decompressed_data = (uint8_t *)malloc(f->decompressed_size+1); uint8_t *decompressed_data = (uint8_t *)malloc(f->decompressed_size+1);
if (!decompressed_data) { if (!decompressed_data) {
return nullptr; return nullptr;
} }
if (f->decompressed_size == 0) { if (f->decompressed_size == 0) {
// empty file // empty file, avoid decompression problems
size = 0; size = 0;
return decompressed_data; return decompressed_data;
} }
// explicitly null-terminate the data
decompressed_data[f->decompressed_size] = 0;
TINF_DATA *d = (TINF_DATA *)malloc(sizeof(TINF_DATA)); TINF_DATA *d = (TINF_DATA *)malloc(sizeof(TINF_DATA));
if (!d) { if (!d) {
::free(decompressed_data); ::free(decompressed_data);
@ -106,7 +103,7 @@ const uint8_t *AP_ROMFS::find_decompress(const char *name, uint32_t &size)
#endif #endif
} }
// free returned data // free decompressed file data
void AP_ROMFS::free(const uint8_t *data) void AP_ROMFS::free(const uint8_t *data)
{ {
#ifndef HAL_ROMFS_UNCOMPRESSED #ifndef HAL_ROMFS_UNCOMPRESSED

View File

@ -7,13 +7,13 @@
class AP_ROMFS { class AP_ROMFS {
public: public:
// find a file and de-compress, assumning gzip format. The // Find the named file and return its decompressed data and size. Caller
// decompressed data will be allocated with malloc(). You must // must call AP_ROMFS::free() on the return value after use to free it.
// call AP_ROMFS::free() on the return value after use. The next byte after // The data is guaranteed to be null-terminated such that it can be
// the file data is guaranteed to be null. // treated as a string.
static const uint8_t *find_decompress(const char *name, uint32_t &size); static const uint8_t *find_decompress(const char *name, uint32_t &size);
// free returned data // free decompressed file data
static void free(const uint8_t *data); static void free(const uint8_t *data);
// get the size of a file without decompressing // get the size of a file without decompressing