Browse Source

[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>
tags/v1.20.1
Michael Brown 11 years ago
parent
commit
49d14f0d8d
1 changed files with 4 additions and 0 deletions
  1. 4
    0
      src/core/base16.c

+ 4
- 0
src/core/base16.c View File

51
 	char *encoded_bytes = encoded;
51
 	char *encoded_bytes = encoded;
52
 	size_t remaining = len;
52
 	size_t remaining = len;
53
 
53
 
54
+	/* Encode each byte */
54
 	for ( ; remaining-- ; encoded_bytes += 2 ) {
55
 	for ( ; remaining-- ; encoded_bytes += 2 ) {
55
 		sprintf ( encoded_bytes, "%02x", *(raw_bytes++) );
56
 		sprintf ( encoded_bytes, "%02x", *(raw_bytes++) );
56
 	}
57
 	}
57
 
58
 
59
+	/* Ensure terminating NUL exists even if length was zero */
60
+	*encoded_bytes = '\0';
61
+
58
 	DBG ( "Base16-encoded to \"%s\":\n", encoded );
62
 	DBG ( "Base16-encoded to \"%s\":\n", encoded );
59
 	DBG_HDA ( 0, raw, len );
63
 	DBG_HDA ( 0, raw, len );
60
 	assert ( strlen ( encoded ) == base16_encoded_len ( len ) );
64
 	assert ( strlen ( encoded ) == base16_encoded_len ( len ) );

Loading…
Cancel
Save