nvdimm.lists.linux.dev archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/3] Avoid filename truncation in numastat
@ 2018-03-22 18:33 Ross Zwisler
  2018-03-22 18:33 ` [PATCH 2/3] readdir_r(3) is deprecated, use readdir(3) instead Ross Zwisler
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Ross Zwisler @ 2018-03-22 18:33 UTC (permalink / raw)
  To: Andi Kleen, linux-nvdimm

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

_______________________________________________
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 2/3] readdir_r(3) is deprecated, use readdir(3) instead
  2018-03-22 18:33 [PATCH 1/3] Avoid filename truncation in numastat Ross Zwisler
@ 2018-03-22 18:33 ` Ross Zwisler
  2018-03-22 18:33 ` [PATCH 3/3] Add pkg-config file for NUMA library Ross Zwisler
  2018-04-05 15:01 ` [PATCH 1/3] Avoid filename truncation in numastat Ross Zwisler
  2 siblings, 0 replies; 4+ messages in thread
From: Ross Zwisler @ 2018-03-22 18:33 UTC (permalink / raw)
  To: Andi Kleen, linux-nvdimm

gcc 7.3.1 provides the following warning when compiling affinity.c:

affinity.c: In function ‘affinity_file’:
affinity.c:158:2: warning: ‘readdir_r’ is deprecated [-Wdeprecated-declarations]
  while (readdir_r(dir, &de, &dep) == 0 && dep) {
  ^~~~~
In file included from affinity.c:39:0:
/usr/include/dirent.h:183:12: note: declared here
 extern int readdir_r (DIR *__restrict __dirp,
            ^~~~~~~~~

According to the man page for readdir_r(3), calls this function should be
fixed to instead use readdir(3).

One interesting note: I had to move the affinity_class() call above the
closedir(dir) call in affinity_file() because with readdir(3) the string
stored in 'name' is cleared on closedir().  This doesn't happen with
readdir_r() for some reason.

Signed-off-by: Ross Zwisler <ross.zwisler@linux.intel.com>
---
 affinity.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/affinity.c b/affinity.c
index 85597fc..06784f7 100644
--- a/affinity.c
+++ b/affinity.c
@@ -131,7 +131,7 @@ static int affinity_file(struct bitmask *mask, char *cls, const char *file)
 	int n;
 	unsigned maj = 0, min = 0;
 	dev_t d;
-	struct dirent de, *dep;
+	struct dirent *dep;
 
 	cls = "block";
 	char fn[sizeof("/sys/class/") + strlen(cls)];
@@ -155,8 +155,10 @@ static int affinity_file(struct bitmask *mask, char *cls, const char *file)
 			  cls);
 		return -1;
 	}
-	while (readdir_r(dir, &de, &dep) == 0 && dep) {
+	while ((dep = readdir(dir)) != NULL) {
 		char *name = dep->d_name;
+		int ret;
+
 		if (*name == '.')
 			continue;
 		char *dev;
@@ -179,8 +181,9 @@ static int affinity_file(struct bitmask *mask, char *cls, const char *file)
 		if (major(d) != maj || minor(d) != min)
 			continue;
 
+		ret = affinity_class(mask, "block", name);
 		closedir(dir);
-		return affinity_class(mask, "block", name);
+		return ret;
 	}
 	closedir(dir);
 	numa_warn(W_blockdev5, "Cannot find block device %x:%x in sysfs for `%s'",
-- 
2.14.3

_______________________________________________
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 3/3] Add pkg-config file for NUMA library
  2018-03-22 18:33 [PATCH 1/3] Avoid filename truncation in numastat Ross Zwisler
  2018-03-22 18:33 ` [PATCH 2/3] readdir_r(3) is deprecated, use readdir(3) instead Ross Zwisler
@ 2018-03-22 18:33 ` Ross Zwisler
  2018-04-05 15:01 ` [PATCH 1/3] Avoid filename truncation in numastat Ross Zwisler
  2 siblings, 0 replies; 4+ messages in thread
From: Ross Zwisler @ 2018-03-22 18:33 UTC (permalink / raw)
  To: Andi Kleen, linux-nvdimm

This is needed so that other projects can add a dependency on libnuma via
PKG_CHECK_MODULES([NUMA], [numa]).  This enabling makes 'make install' do
the right thing, and of course individual distros will need to add enabling
to their associated packages (rpm, deb, etc.) so the package manager
installs do the right thing.

Signed-off-by: Ross Zwisler <ross.zwisler@linux.intel.com>
---
 .gitignore  |  3 +++
 Makefile.am | 18 ++++++++++++++++++
 numa.pc.in  | 10 ++++++++++
 3 files changed, 31 insertions(+)
 create mode 100644 numa.pc.in

diff --git a/.gitignore b/.gitignore
index 68d5235..8cfe9f8 100644
--- a/.gitignore
+++ b/.gitignore
@@ -59,3 +59,6 @@
 /test-suite.log
 /test/*.log
 /test/*.trs
+
+# pkg-config file
+numa.pc
diff --git a/Makefile.am b/Makefile.am
index b6db339..ac4dc3c 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1,5 +1,6 @@
 
 ACLOCAL_AMFLAGS = -I m4
+CLEANFILES =
 
 AM_CPPFLAGS = -Wall
 
@@ -141,3 +142,20 @@ TESTS = \
 # These are known to be broken:
 #	test/prefered
 #	test/randmap
+
+SED_PROCESS = \
+        $(AM_V_GEN)$(SED) \
+        -e 's,@VERSION\@,$(VERSION),g' \
+        -e 's,@prefix\@,$(prefix),g' \
+        -e 's,@exec_prefix\@,$(exec_prefix),g' \
+        -e 's,@libdir\@,$(libdir),g' \
+        -e 's,@includedir\@,$(includedir),g' \
+        < $< > $@ || rm $@
+
+%.pc: %.pc.in Makefile
+	$(SED_PROCESS)
+
+pkgconfigdir = $(libdir)/pkgconfig
+pkgconfig_DATA = numa.pc
+EXTRA_DIST += numa.pc.in
+CLEANFILES += numa.pc
diff --git a/numa.pc.in b/numa.pc.in
new file mode 100644
index 0000000..8a0f202
--- /dev/null
+++ b/numa.pc.in
@@ -0,0 +1,10 @@
+prefix=@prefix@
+exec_prefix=@exec_prefix@
+libdir=@libdir@
+includedir=@includedir@
+
+Name: numa
+Description: NUMA policy library
+Version: @VERSION@
+Cflags: -I${includedir}
+Libs: -L${libdir} -lnuma
-- 
2.14.3

_______________________________________________
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH 1/3] Avoid filename truncation in numastat
  2018-03-22 18:33 [PATCH 1/3] Avoid filename truncation in numastat Ross Zwisler
  2018-03-22 18:33 ` [PATCH 2/3] readdir_r(3) is deprecated, use readdir(3) instead Ross Zwisler
  2018-03-22 18:33 ` [PATCH 3/3] Add pkg-config file for NUMA library Ross Zwisler
@ 2018-04-05 15:01 ` Ross Zwisler
  2 siblings, 0 replies; 4+ messages in thread
From: Ross Zwisler @ 2018-04-05 15:01 UTC (permalink / raw)
  To: Andi Kleen; +Cc: Andi Kleen, linux-nvdimm

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
> 
_______________________________________________
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2018-04-05 15:01 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-03-22 18:33 [PATCH 1/3] Avoid filename truncation in numastat Ross Zwisler
2018-03-22 18:33 ` [PATCH 2/3] readdir_r(3) is deprecated, use readdir(3) instead Ross Zwisler
2018-03-22 18:33 ` [PATCH 3/3] Add pkg-config file for NUMA library Ross Zwisler
2018-04-05 15:01 ` [PATCH 1/3] Avoid filename truncation in numastat Ross Zwisler

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).