浏览代码

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

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

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 ) );

正在加载...
取消
保存