xfs
[Top] [All Lists]

[PATCH 2/6] xfsprogs: libxcmd: avoid using strtok()

To: xfs@xxxxxxxxxxx
Subject: [PATCH 2/6] xfsprogs: libxcmd: avoid using strtok()
From: Alex Elder <aelder@xxxxxxx>
Date: Mon, 3 Oct 2011 07:49:16 -0500
Cc: Alex Elder <aelder@xxxxxxx>
In-reply-to: <1317646160-5437-1-git-send-email-aelder@xxxxxxx>
In-reply-to: <4a7a9e630aa7c62357a606f762abc19fc1d7073b.1317646036.git.aelder@xxxxxxx>
References: <1317646160-5437-1-git-send-email-aelder@xxxxxxx>
References: <4a7a9e630aa7c62357a606f762abc19fc1d7073b.1317646036.git.aelder@xxxxxxx>
The strtok() library routine overwrites delimiting bytes in the
string it is supplied.  It is also not length-constrained.

Since we're making a duplicate of the string anyway, and since we
are only finding the end of a single token, we can do both without
the need to modify the passed-in mount entry structure.

Add checking for memory allocation failures, and if one occurs just
exit (as is the practice elsewhere in this file).

Signed-off-by: Alex Elder <aelder@xxxxxxx>
---
 libxcmd/paths.c |   17 +++++++++++++----
 1 files changed, 13 insertions(+), 4 deletions(-)

diff --git a/libxcmd/paths.c b/libxcmd/paths.c
index ae9db32..ed93110 100644
--- a/libxcmd/paths.c
+++ b/libxcmd/paths.c
@@ -216,12 +216,21 @@ fs_extract_mount_options(
 
        /* Do this only after we've finished processing mount options */
        if (fslog) {
-               strtok(fslog, " ,");
-               fslog = strdup(fslog);
+               fslog = strndup(fslog, strcspn(fslog, " ,"));
+               if (!fslog) {
+                       fprintf(stderr, _("%s: %s: out of memory (fslog)\n"),
+                                   progname, __func__);
+                       exit(1);
+               }
        }
        if (fsrt) {
-               strtok(fsrt, " ,");
-               fsrt = strdup(fsrt);
+               fsrt = strndup(fsrt, strcspn(fsrt, " ,"));
+               if (!fsrt) {
+                       fprintf(stderr, _("%s: %s: out of memory (fsrt)\n"),
+                                   progname, __func__);
+                       free(fslog);
+                       exit(1);
+               }
        }
 
        *logp = fslog;
-- 
1.7.6.2

<Prev in Thread] Current Thread [Next in Thread>