Explorar el Código

[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 hace 11 años
padre
commit
49d14f0d8d
Se han modificado 1 ficheros con 4 adiciones y 0 borrados
  1. 4
    0
      src/core/base16.c

+ 4
- 0
src/core/base16.c Ver fichero

@@ -51,10 +51,14 @@ void base16_encode ( const uint8_t *raw, size_t len, char *encoded ) {
51 51
 	char *encoded_bytes = encoded;
52 52
 	size_t remaining = len;
53 53
 
54
+	/* Encode each byte */
54 55
 	for ( ; remaining-- ; encoded_bytes += 2 ) {
55 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 62
 	DBG ( "Base16-encoded to \"%s\":\n", encoded );
59 63
 	DBG_HDA ( 0, raw, len );
60 64
 	assert ( strlen ( encoded ) == base16_encoded_len ( len ) );

Loading…
Cancelar
Guardar