diff mbox

[1/3] Avoid filename truncation in numastat

Message ID 20180322183322.23076-1-ross.zwisler@linux.intel.com (mailing list archive)
State New, archived
Headers show

Commit Message

Ross Zwisler March 22, 2018, 6:33 p.m. UTC
gcc 7.3.1 provides the following warning when compiling numastat.c:

numastat.c: In function ‘add_pids_from_pattern_search’:
numastat.c:1316:41: warning: ‘%s’ directive output may be truncated writing
up to 255 bytes into a region of size 58 [-Wformat-truncation=]
   snprintf(fname, sizeof(fname), "/proc/%s/cmdline", namelist[ix]->d_name);
                                         ^~
numastat.c:1316:3: note: ‘snprintf’ output between 15 and 270 bytes into a
destination of size 64
   snprintf(fname, sizeof(fname), "/proc/%s/cmdline", namelist[ix]->d_name);
   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

This is valid - namelist[ix]->d_name is size 256 bytes, we have some extra
bytes as part of our format string.  Our destination buffer, 'fname', is
only 64 bytes wide.

Signed-off-by: Ross Zwisler <ross.zwisler@linux.intel.com>
---
 numastat.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Ross Zwisler April 5, 2018, 3:01 p.m. UTC | #1
Ping on this series.

On Thu, Mar 22, 2018 at 12:33:20PM -0600, Ross Zwisler wrote:
> gcc 7.3.1 provides the following warning when compiling numastat.c:
> 
> numastat.c: In function ‘add_pids_from_pattern_search’:
> numastat.c:1316:41: warning: ‘%s’ directive output may be truncated writing
> up to 255 bytes into a region of size 58 [-Wformat-truncation=]
>    snprintf(fname, sizeof(fname), "/proc/%s/cmdline", namelist[ix]->d_name);
>                                          ^~
> numastat.c:1316:3: note: ‘snprintf’ output between 15 and 270 bytes into a
> destination of size 64
>    snprintf(fname, sizeof(fname), "/proc/%s/cmdline", namelist[ix]->d_name);
>    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> 
> This is valid - namelist[ix]->d_name is size 256 bytes, we have some extra
> bytes as part of our format string.  Our destination buffer, 'fname', is
> only 64 bytes wide.
> 
> Signed-off-by: Ross Zwisler <ross.zwisler@linux.intel.com>
> ---
>  numastat.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/numastat.c b/numastat.c
> index e0a5639..2d413df 100644
> --- a/numastat.c
> +++ b/numastat.c
> @@ -1312,7 +1312,7 @@ void add_pids_from_pattern_search(char *pattern) {
>  		}
>  		// Next copy cmdline file contents onto end of buffer.  Do it a
>  		// character at a time to convert nulls to spaces.
> -		char fname[64];
> +		char fname[272];
>  		snprintf(fname, sizeof(fname), "/proc/%s/cmdline", namelist[ix]->d_name);
>  		FILE *fs = fopen(fname, "r");
>  		if (fs) {
> -- 
> 2.14.3
>
diff mbox

Patch

diff --git a/numastat.c b/numastat.c
index e0a5639..2d413df 100644
--- a/numastat.c
+++ b/numastat.c
@@ -1312,7 +1312,7 @@  void add_pids_from_pattern_search(char *pattern) {
 		}
 		// Next copy cmdline file contents onto end of buffer.  Do it a
 		// character at a time to convert nulls to spaces.
-		char fname[64];
+		char fname[272];
 		snprintf(fname, sizeof(fname), "/proc/%s/cmdline", namelist[ix]->d_name);
 		FILE *fs = fopen(fname, "r");
 		if (fs) {