diff options
author | Mark Wielaard <[email protected]> | 2020-06-09 18:21:36 +0200 |
---|---|---|
committer | Mark Wielaard <[email protected]> | 2020-06-09 18:21:36 +0200 |
commit | c0d643e7d91fc002c9fecd83277c62a0e56ef76f (patch) | |
tree | 89b5cf6673baa2cb206fdf245f4e30fe884945ad /libelf/elf_compress.c | |
parent | 14030673602a4f331f348decd51e5f0160719f0e (diff) | |
parent | 5643e037cb7a38ed5d52f50421be706ea8014e3d (diff) |
Merge tag 'elfutils-0.177' into mjw/RH-DTSdts-0.177
elfutils 0.177 release
Conflicts:
libebl/eblopenbackend.c Removed try_dlopen and csky.
tests/run-strip-reloc.sh Removed csky tests.
Diffstat (limited to 'libelf/elf_compress.c')
-rw-r--r-- | libelf/elf_compress.c | 33 |
1 files changed, 19 insertions, 14 deletions
diff --git a/libelf/elf_compress.c b/libelf/elf_compress.c index be9eeaba..244467b5 100644 --- a/libelf/elf_compress.c +++ b/libelf/elf_compress.c @@ -44,17 +44,17 @@ /* Cleanup and return result. Don't leak memory. */ static void * do_deflate_cleanup (void *result, z_stream *z, void *out_buf, - int ei_data, Elf_Data *cdatap) + Elf_Data *cdatap) { deflateEnd (z); free (out_buf); - if (ei_data != MY_ELFDATA) + if (cdatap != NULL) free (cdatap->d_buf); return result; } -#define deflate_cleanup(result) \ - do_deflate_cleanup(result, &z, out_buf, ei_data, &cdata) +#define deflate_cleanup(result, cdata) \ + do_deflate_cleanup(result, &z, out_buf, cdata) /* Given a section, uses the (in-memory) Elf_Data to extract the original data size (including the given header size) and data @@ -127,7 +127,8 @@ __libelf_compress (Elf_Scn *scn, size_t hsize, int ei_data, { /* Convert to raw if different endianess. */ cdata = *data; - if (ei_data != MY_ELFDATA) + bool convert = ei_data != MY_ELFDATA && data->d_size > 0; + if (convert) { /* Don't do this conversion in place, we might want to keep the original data around, caller decides. */ @@ -135,10 +136,10 @@ __libelf_compress (Elf_Scn *scn, size_t hsize, int ei_data, if (cdata.d_buf == NULL) { __libelf_seterrno (ELF_E_NOMEM); - return deflate_cleanup (NULL); + return deflate_cleanup (NULL, NULL); } if (gelf_xlatetof (scn->elf, &cdata, data, ei_data) == NULL) - return deflate_cleanup (NULL); + return deflate_cleanup (NULL, &cdata); } z.avail_in = cdata.d_size; @@ -164,7 +165,7 @@ __libelf_compress (Elf_Scn *scn, size_t hsize, int ei_data, if (zrc == Z_STREAM_ERROR) { __libelf_seterrno (ELF_E_COMPRESS_ERROR); - return deflate_cleanup (NULL); + return deflate_cleanup (NULL, convert ? &cdata : NULL); } used += (out_size - used) - z.avail_out; @@ -172,7 +173,7 @@ __libelf_compress (Elf_Scn *scn, size_t hsize, int ei_data, compression forced and we are using more compressed data than original data. */ if (!force && flush == Z_FINISH && used >= *orig_size) - return deflate_cleanup ((void *) -1); + return deflate_cleanup ((void *) -1, convert ? &cdata : NULL); if (z.avail_out == 0) { @@ -180,7 +181,7 @@ __libelf_compress (Elf_Scn *scn, size_t hsize, int ei_data, if (bigger == NULL) { __libelf_seterrno (ELF_E_NOMEM); - return deflate_cleanup (NULL); + return deflate_cleanup (NULL, convert ? &cdata : NULL); } out_buf = bigger; out_size += block; @@ -188,7 +189,7 @@ __libelf_compress (Elf_Scn *scn, size_t hsize, int ei_data, } while (z.avail_out == 0); /* Need more output buffer. */ - if (ei_data != MY_ELFDATA) + if (convert) { free (cdata.d_buf); cdata.d_buf = NULL; @@ -200,7 +201,7 @@ __libelf_compress (Elf_Scn *scn, size_t hsize, int ei_data, if (zrc != Z_OK) { __libelf_seterrno (ELF_E_COMPRESS_ERROR); - return deflate_cleanup (NULL); + return deflate_cleanup (NULL, NULL); } *new_size = used; @@ -220,7 +221,11 @@ __libelf_decompress (void *buf_in, size_t size_in, size_t size_out) return NULL; } - void *buf_out = malloc (size_out); + /* Malloc might return NULL when requestion zero size. This is highly + unlikely, it would only happen when the compression was forced. + But we do need a non-NULL buffer to return and set as result. + Just make sure to always allocate at least 1 byte. */ + void *buf_out = malloc (size_out ?: 1); if (unlikely (buf_out == NULL)) { __libelf_seterrno (ELF_E_NOMEM); @@ -308,7 +313,7 @@ __libelf_reset_rawdata (Elf_Scn *scn, void *buf, size_t size, size_t align, { /* This is the new raw data, replace and possibly free old data. */ scn->rawdata.d.d_off = 0; - scn->rawdata.d.d_version = __libelf_version; + scn->rawdata.d.d_version = EV_CURRENT; scn->rawdata.d.d_buf = buf; scn->rawdata.d.d_size = size; scn->rawdata.d.d_align = align; |