diff options
author | Michael Brown <mcb30@ipxe.org> | 2013-07-15 00:06:45 +0200 |
---|---|---|
committer | Michael Brown <mcb30@ipxe.org> | 2013-07-15 00:06:45 +0200 |
commit | 49d14f0d8d9291e3a98c6bfd9005b3c6bb85e6c8 (patch) | |
tree | 1b6fcb109dd488870b717e96ab701f3a787d7f5d /src/core/base16.c | |
parent | 6ad05aa319f69c110ffe3bdd1bbb67b5f778cbe3 (diff) | |
download | ipxe-49d14f0d8d9291e3a98c6bfd9005b3c6bb85e6c8.tar.gz |
[base16] Ensure base16_encode() always terminates its result string
base16_encode() will fail to generate a terminating NUL if the length
of the raw data is zero, since the loop calling sprintf() will never
execute.
Fix by explicitly terminating the result with a NUL.
Reported-by: Marin Hannache <git@mareo.fr>
Debugged-by: Marin Hannache <git@mareo.fr>
Tested-by: Marin Hannache <git@mareo.fr>
Signed-off-by: Michael Brown <mcb30@ipxe.org>
Diffstat (limited to 'src/core/base16.c')
-rw-r--r-- | src/core/base16.c | 4 |
1 files changed, 4 insertions, 0 deletions
diff --git a/src/core/base16.c b/src/core/base16.c index 7fa4b200..2544bd7f 100644 --- a/src/core/base16.c +++ b/src/core/base16.c @@ -51,10 +51,14 @@ void base16_encode ( const uint8_t *raw, size_t len, char *encoded ) { char *encoded_bytes = encoded; size_t remaining = len; + /* Encode each byte */ for ( ; remaining-- ; encoded_bytes += 2 ) { sprintf ( encoded_bytes, "%02x", *(raw_bytes++) ); } + /* Ensure terminating NUL exists even if length was zero */ + *encoded_bytes = '\0'; + DBG ( "Base16-encoded to \"%s\":\n", encoded ); DBG_HDA ( 0, raw, len ); assert ( strlen ( encoded ) == base16_encoded_len ( len ) ); |