Util-Linux package development
 help / color / mirror / Atom feed
From: Ruediger Meier <sweet_f_a@gmx.de>
To: util-linux@vger.kernel.org
Subject: [PATCH 1/2] ipcs: make sure to parse whole lines for shm_data
Date: Fri, 18 Mar 2016 12:49:17 +0100	[thread overview]
Message-ID: <1458301758-21853-2-git-send-email-sweet_f_a@gmx.de> (raw)
In-Reply-To: <1458301758-21853-1-git-send-email-sweet_f_a@gmx.de>

From: Ruediger Meier <ruediger.meier@ga-group.nl>

We want to parse 16 columns _per_row_ without mixing them up. The
existing code is unsafe for more or less columns and could even
run into endless loops. This patch assures that we parse row-wise
and really skip lines with columns != 16.

Probably somehow we could have also done this with fscanf() only.
Using fgets() additionally makes the code more easy to read and
to improve later.

Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
---
 sys-utils/ipcutils.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/sys-utils/ipcutils.c b/sys-utils/ipcutils.c
index 400f00c..c763cde 100644
--- a/sys-utils/ipcutils.c
+++ b/sys-utils/ipcutils.c
@@ -99,6 +99,7 @@ int ipc_shm_get_info(int id, struct shm_data **shmds)
 {
 	FILE *f;
 	int i = 0, maxid;
+	char buf[BUFSIZ];
 	struct shm_data *p;
 	struct shmid_ds dummy;
 
@@ -111,8 +112,8 @@ int ipc_shm_get_info(int id, struct shm_data **shmds)
 
 	while (fgetc(f) != '\n');		/* skip header */
 
-	while (feof(f) == 0) {
-		if (fscanf(f,
+	while (fgets(buf, sizeof(buf), f) != NULL) {
+		if (sscanf(buf,
 			  "%d %d  %o %"SCNu64 " %u %u  "
 			  "%"SCNu64 " %u %u %u %u %"SCNi64 " %"SCNi64 " %"SCNi64
 			  " %"SCNu64 " %"SCNu64 "\n",
@@ -132,7 +133,7 @@ int ipc_shm_get_info(int id, struct shm_data **shmds)
 			   &p->shm_ctim,
 			   &p->shm_rss,
 			   &p->shm_swp) != 16)
-			continue;
+			continue; /* ivalid line, skipped */
 
 		if (id > -1) {
 			/* ID specified */
-- 
1.8.4.5


  reply	other threads:[~2016-03-18 11:49 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-03-18 11:49 [PATCH 0/2] ipcs: more robust and compatible /proc parsing Ruediger Meier
2016-03-18 11:49 ` Ruediger Meier [this message]
2016-03-18 11:49 ` [PATCH 2/2] ipcs: --shmems, upward/backward compatibility Ruediger Meier
2016-03-18 13:48 ` [PATCH 0/2] ipcs: more robust and compatible /proc parsing Karel Zak
2016-03-18 14:03   ` Ruediger Meier
2016-03-18 14:38     ` Karel Zak

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1458301758-21853-2-git-send-email-sweet_f_a@gmx.de \
    --to=sweet_f_a@gmx.de \
    --cc=util-linux@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox