瀏覽代碼

[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 年之前
父節點
當前提交
49d14f0d8d
共有 1 個文件被更改,包括 4 次插入0 次删除
  1. 4
    0
      src/core/base16.c

+ 4
- 0
src/core/base16.c 查看文件

@@ -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…
取消
儲存