Skip to content

Commit

Permalink
[base16] Ensure base16_encode() always terminates its result string
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
mcb30 committed Jul 14, 2013
1 parent 6ad05aa commit 49d14f0
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/core/base16.c
Expand Up @@ -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 ) );
Expand Down

0 comments on commit 49d14f0

Please sign in to comment.