Skip to contentSkip to conversation

libselinux: silence -Wstringop-overflow warning from gcc 10.3.1

PATCH In review Nicolas Iooss 2 participants 3 messages Apr 30, 2021 – May 12, 2021 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libselinux: silence -Wstringop-overflow warning from gcc 10.3.1

When building libselinux on Fedora 33 with gcc 10.3.1, the compiler reports:

label_file.c: In function ‘lookup_all.isra’:
label_file.c:940:4: error: ‘strncpy’ specified bound depends on the length of the source argument [-Werror=stringop-overflow=]
940 | strncpy(clean_key, key, len - 1);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
label_file.c:927:8: note: length computed here
927 | len = strlen(key);
| ^~~~~~~~~~~
cc1: all warnings being treated as errors

As clean_key is the result of malloc(len), there is no issue here. But using strncpy can be considered as strange, because the size of the string is already known and the NUL terminator is always added later, in function ‘lookup_all.isra.

Replace strncpy with memcpy to silence this gcc false-positive warning.

---
 libselinux/src/label_file.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libselinux/src/label_file.c +1 -1
index 726394ca4332..cfce23e0119e 100644
@@ -909,7 +909,7 @@ static const struct spec **lookup_all(struct selabel_handle *rec,
 			if (!clean_key)
 				goto finish;
 
-			strncpy(clean_key, key, len - 1);
+			memcpy(clean_key, key, len - 1);
 		}
 
 		clean_key[len - 1] = '\0';
Signature · 1 line
2.31.0
Acked-by on v1 1/1
#1 lore raw
Quoted 13 lines · Nicolas Iooss

When building libselinux on Fedora 33 with gcc 10.3.1, the compiler reports:

label_file.c: In function ‘lookup_all.isra’:
label_file.c:940:4: error: ‘strncpy’ specified bound depends on the length of the source argument [-Werror=stringop-overflow=]
940 | strncpy(clean_key, key, len - 1);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
label_file.c:927:8: note: length computed here
927 | len = strlen(key);
| ^~~~~~~~~~~
cc1: all warnings being treated as errors

Quoted 8 lines

As clean_key is the result of malloc(len), there is no issue here. But using strncpy can be considered as strange, because the size of the string is already known and the NUL terminator is always added later, in function ‘lookup_all.isra.

Replace strncpy with memcpy to silence this gcc false-positive warning.

I wasn't able to reproduce it with gcc-11.0.1-0.7.fc35.x86_64, but it's indeed reproducible with gcc-10.3.1-1.fc33.x86_64 and this patch fixes it.

Quoted 19 lines
---
 libselinux/src/label_file.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libselinux/src/label_file.c +1 -1
index 726394ca4332..cfce23e0119e 100644
@@ -909,7 +909,7 @@ static const struct spec **lookup_all(struct selabel_handle *rec,
 			if (!clean_key)
 				goto finish;
 
-			strncpy(clean_key, key, len - 1);
+			memcpy(clean_key, key, len - 1);
 		}
 
 		clean_key[len - 1] = '\0';
Signature · 1 line
2.31.0
#2 lore raw
Quoted 22 lines · Petr Lautrbach
Quoted 20 lines · Nicolas Iooss

When building libselinux on Fedora 33 with gcc 10.3.1, the compiler reports:

label_file.c: In function ‘lookup_all.isra’:
label_file.c:940:4: error: ‘strncpy’ specified bound depends on the length of the source argument [-Werror=stringop-overflow=]
940 | strncpy(clean_key, key, len - 1);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
label_file.c:927:8: note: length computed here
927 | len = strlen(key);
| ^~~~~~~~~~~
cc1: all warnings being treated as errors

As clean_key is the result of malloc(len), there is no issue here. But using strncpy can be considered as strange, because the size of the string is already known and the NUL terminator is always added later, in function ‘lookup_all.isra.

Replace strncpy with memcpy to silence this gcc false-positive warning.

Quoted 8 lines
Quoted 1 line

I wasn't able to reproduce it with gcc-11.0.1-0.7.fc35.x86_64, but it's indeed reproducible with gcc-10.3.1-1.fc33.x86_64 and this patch fixes it.

Merged

Quoted 19 lines
Quoted 19 lines
---
 libselinux/src/label_file.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libselinux/src/label_file.c +1 -1
index 726394ca4332..cfce23e0119e 100644
@@ -909,7 +909,7 @@ static const struct spec **lookup_all(struct selabel_handle *rec,
 			if (!clean_key)
 				goto finish;
 
-			strncpy(clean_key, key, len - 1);
+			memcpy(clean_key, key, len - 1);
 		}
 
 		clean_key[len - 1] = '\0';
Signature · 1 line
2.31.0