Skip to content

Commit 95cadae

Browse files
Qian Caikees
authored andcommitted
fortify: strlen: Avoid shadowing previous locals
The __compiletime_strlen() macro expansion will shadow p_size and p_len local variables. No callers currently use any of the shadowed names for their "p" variable, so there are no code generation problems. Add "__" prefixes to variable definitions __compiletime_strlen() to avoid new W=2 warnings: ./include/linux/fortify-string.h: In function 'strnlen': ./include/linux/fortify-string.h:17:9: warning: declaration of 'p_size' shadows a previous local [-Wshadow] 17 | size_t p_size = __builtin_object_size(p, 1); \ | ^~~~~~ ./include/linux/fortify-string.h:77:17: note: in expansion of macro '__compiletime_strlen' 77 | size_t p_len = __compiletime_strlen(p); | ^~~~~~~~~~~~~~~~~~~~ ./include/linux/fortify-string.h:76:9: note: shadowed declaration is here 76 | size_t p_size = __builtin_object_size(p, 1); | ^~~~~~ Signed-off-by: Qian Cai <[email protected]> Signed-off-by: Kees Cook <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 9a48e75 commit 95cadae

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

include/linux/fortify-string.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@ void __read_overflow(void) __compiletime_error("detected read beyond size of obj
1010
void __read_overflow2(void) __compiletime_error("detected read beyond size of object (2nd parameter)");
1111
void __write_overflow(void) __compiletime_error("detected write beyond size of object (1st parameter)");
1212

13-
#define __compiletime_strlen(p) \
14-
({ \
15-
unsigned char *__p = (unsigned char *)(p); \
16-
size_t ret = (size_t)-1; \
17-
size_t p_size = __builtin_object_size(p, 1); \
18-
if (p_size != (size_t)-1) { \
19-
size_t p_len = p_size - 1; \
20-
if (__builtin_constant_p(__p[p_len]) && \
21-
__p[p_len] == '\0') \
22-
ret = __builtin_strlen(__p); \
23-
} \
24-
ret; \
13+
#define __compiletime_strlen(p) \
14+
({ \
15+
unsigned char *__p = (unsigned char *)(p); \
16+
size_t __ret = (size_t)-1; \
17+
size_t __p_size = __builtin_object_size(p, 1); \
18+
if (__p_size != (size_t)-1) { \
19+
size_t __p_len = __p_size - 1; \
20+
if (__builtin_constant_p(__p[__p_len]) && \
21+
__p[__p_len] == '\0') \
22+
__ret = __builtin_strlen(__p); \
23+
} \
24+
__ret; \
2525
})
2626

2727
#if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)

0 commit comments

Comments
 (0)