Skip to content

Commit 8233336

Browse files
committed
fake_bsp: fix stringop-overread warnings with string literals
GCC 11 correctly points out that there is no point in limiting strnlen() to a size that is greater than the length of the string literal argument. test/fake_bsp/fakegoodbsp.cpp:47:26: warning: 'size_t strnlen(const char*, size_t)' specified bound 1204 exceeds source size 16 [-Wstringop-overread] 47 | size_t Xlen = strnlen(X, MAX_NAME_SIZE) + 1; \ | ~~~~~~~^~~~~~~~~~~~~~~~~~ strnlen() is commonly used with an input buffer that is possibly not null-terminated, i.e., contains a truncated string. strnlen() is also useful to set an upper bound on the length to consume, e.g., to avoid a denial of service. Neither of these use cases apply here. MAX_NAME_SIZE was previously introduced in acl.h to address Klocwork issues relating to missing null termination of strings. The chosen 1204 is a typo of 1024, but even then, it should be 1023 such that a buffer with the trailing null byte occupies 1023 + 1 = 1024 bytes. Signed-off-by: Peter Colberg <peter.colberg@intel.com>
1 parent d707cc2 commit 8233336

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

test/fake_bsp/fakegoodbsp.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ typedef enum {
4444
}
4545
#define RESULT_STR(X) \
4646
do { \
47-
size_t Xlen = strnlen(X, MAX_NAME_SIZE) + 1; \
47+
size_t Xlen = strlen(X) + 1; \
4848
memcpy((void *)param_value, X, \
4949
(param_value_size <= Xlen) ? param_value_size : Xlen); \
5050
if (param_size_ret) \
@@ -283,7 +283,7 @@ AOCL_MMD_CALL int aocl_mmd_read(int handle, aocl_mmd_op_t op, size_t len,
283283
(unsigned int)offset);
284284
return -1;
285285
case OFFSET_CONFIGURATION_ROM:
286-
if (strnlen(config_str, MAX_NAME_SIZE) <= len) {
286+
if (strlen(config_str) <= len) {
287287
memcpy(dst, (void *)config_str, len);
288288
return 0;
289289
} else {

test/fake_bsp/missingfuncbsp.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ typedef enum {
4444
}
4545
#define RESULT_STR(X) \
4646
do { \
47-
size_t Xlen = strnlen(X, MAX_NAME_SIZE) + 1; \
47+
size_t Xlen = strlen(X) + 1; \
4848
memcpy((void *)param_value, X, \
4949
(param_value_size <= Xlen) ? param_value_size : Xlen); \
5050
if (param_size_ret) \
@@ -268,7 +268,7 @@ AOCL_MMD_CALL int aocl_mmd_read(int handle, aocl_mmd_op_t op, size_t len,
268268
(unsigned int)offset);
269269
return -1;
270270
case OFFSET_CONFIGURATION_ROM:
271-
if (strnlen(config_str, MAX_NAME_SIZE) <= len) {
271+
if (strlen(config_str) <= len) {
272272
memcpy(dst, (void *)config_str, len);
273273
return 0;
274274
} else {

0 commit comments

Comments
 (0)