mirror of
https://github.com/ibm-s390-linux/s390-tools.git
synced 2026-08-05 02:14:52 +00:00
dasdinfo: Fix truncation warning
Commit3c80f7e025("dasdinfo: fix buffer overflow warning") changed a sprintf call to snprintf to avoid a buffer overflow warning. However, GCC 7 now warns about a potential truncation with snprintf: dasdinfo.c: In function 'main': dasdinfo.c:577:18: warning: '%s' directive output may be truncated writing up to 255 bytes into a region of size 69 [-Wformat-truncation=] "/sys/block/%s/device/uid", dir_entry->d_name); ^~ dasdinfo.c:576:4: note: 'snprintf' output between 23 and 278 bytes into a destination of size 80 snprintf(*uidfile, RD_BUFFER_SIZE, ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ "/sys/block/%s/device/uid", dir_entry->d_name); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ We could get around this by increasing the buffer. Though, the current buffer size is already plenty and we know better anyway. Avoid the warning by simply checking the return value of snprintf and display an error in case data was truncated nonetheless. Fixes:3c80f7e025("dasdinfo: fix buffer overflow warning") Signed-off-by: Jan Höppner <hoeppner@linux.vnet.ibm.com> Signed-off-by: Michael Holzheu <holzheu@linux.vnet.ibm.com>
This commit is contained in:
committed by
Michael Holzheu
parent
31866fbfa4
commit
1261105fe2
@@ -545,6 +545,7 @@ static int dinfo_get_uid_from_devnode(char **uidfile, char *devnode)
|
||||
char *readbuf;
|
||||
DIR *directory = NULL;
|
||||
struct dirent *dir_entry = NULL;
|
||||
int rc = 0;
|
||||
|
||||
if (stat(devnode, &stat_buffer) != 0) {
|
||||
printf("Error: could not stat %s\n", devnode);
|
||||
@@ -573,8 +574,15 @@ static int dinfo_get_uid_from_devnode(char **uidfile, char *devnode)
|
||||
|
||||
if (strncmp(stat_dev, readbuf,
|
||||
MAX(strlen(stat_dev), strlen(readbuf)-1)) == 0) {
|
||||
snprintf(*uidfile, RD_BUFFER_SIZE,
|
||||
"/sys/block/%s/device/uid", dir_entry->d_name);
|
||||
rc = snprintf(*uidfile, RD_BUFFER_SIZE,
|
||||
"/sys/block/%s/device/uid",
|
||||
dir_entry->d_name);
|
||||
if (rc >= RD_BUFFER_SIZE) {
|
||||
fprintf(stderr,
|
||||
"Error: Device name was truncated\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user