From: Bernd Schubert <bschubert@q-leap.de>
To: linux-ext4@vger.kernel.org
Subject: [PATCH] e2fsck/e2fsprogs: use sscanf() instead of atoi() in the option parser
Date: Wed, 01 Aug 2007 16:50:04 +0200 [thread overview]
Message-ID: <f8q6is$tg3$1@sea.gmane.org> (raw)
[-- Attachment #1: Type: text/plain, Size: 288 bytes --]
[resent since the first mail doesn't seem to make it to this list]
Hi,
using atoi() should be avoided in the option parser since it doesn't check
for errors. I almost destroyed an important filesystem since I
specified "e2fsck -C -n" and -n was parsed as option to -C
Cheers,
Bernd
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: sscanf_instead_of_atoi.patch --]
[-- Type: text/x-diff; name="sscanf_instead_of_atoi.patch", Size: 2247 bytes --]
diff -r 7b057872ec06 e2fsck/unix.c
--- a/e2fsck/unix.c Fri Jul 27 16:08:50 2007 +0200
+++ b/e2fsck/unix.c Tue Jul 31 12:20:22 2007 +0200
@@ -585,6 +585,10 @@ static errcode_t PRS(int argc, char *arg
#endif
char *extended_opts = 0;
char *cp;
+ int res; /* result of sscanf */
+#ifdef CONFIG_JBD_DEBUG
+ char *jbd_debug;
+#endif
retval = e2fsck_allocate_context(&ctx);
if (retval)
@@ -614,7 +618,10 @@ static errcode_t PRS(int argc, char *arg
switch (c) {
case 'C':
ctx->progress = e2fsck_update_progress;
- ctx->progress_fd = atoi(optarg);
+ res = sscanf(optarg, "%d", &ctx->progress_fd);
+ if (res != 1)
+ goto sscanf_err;
+
if (!ctx->progress_fd)
break;
/* Validate the file descriptor to avoid disasters */
@@ -674,20 +681,26 @@ static errcode_t PRS(int argc, char *arg
/* What we do by default, anyway! */
break;
case 'b':
- ctx->use_superblock = atoi(optarg);
+ res = sscanf(optarg, "%d", &ctx->use_superblock);
+ if (res != 1)
+ goto sscanf_err;
ctx->flags |= E2F_FLAG_SB_SPECIFIED;
break;
case 'B':
ctx->blocksize = atoi(optarg);
break;
case 'I':
- ctx->inode_buffer_blocks = atoi(optarg);
+ res = sscanf(optarg, "%d", &ctx->inode_buffer_blocks);
+ if (res != 1)
+ goto sscanf_err;
break;
case 'j':
ctx->journal_name = string_copy(ctx, optarg, 0);
break;
case 'P':
- ctx->process_inode_size = atoi(optarg);
+ res = sscanf(optarg, "%d", &ctx->process_inode_size);
+ if (res != 1)
+ goto sscanf_err;
break;
case 'L':
replace_bad_blocks++;
@@ -830,10 +843,22 @@ static errcode_t PRS(int argc, char *arg
putenv(newpath);
}
#ifdef CONFIG_JBD_DEBUG
- if (getenv("E2FSCK_JBD_DEBUG"))
- journal_enable_debug = atoi(getenv("E2FSCK_JBD_DEBUG"));
+ jbd_debug = getenv("E2FSCK_JBD_DEBUG");
+ if (jbd_debug)
+ res = sscanf(jbd_debug, "%d", &journal_enable_debug);
+ if (res != 1) {
+ fprintf(stderr,
+ _("\nInvalid argument \"%s\", not an integer\n\n"),
+ jbd_debug);
+ exit (1);
+ }
#endif
return 0;
+
+sscanf_err:
+ fprintf(stderr, _("\nInvalid argument \"%s\", not an integer\n\n"),
+ optarg);
+ exit (1);
}
static const char *my_ver_string = E2FSPROGS_VERSION;
next reply other threads:[~2007-08-01 14:50 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-08-01 14:50 Bernd Schubert [this message]
2007-08-04 2:19 ` [PATCH] e2fsck/e2fsprogs: use sscanf() instead of atoi() in the option parser Theodore Tso
2007-08-06 10:53 ` Bernd Schubert
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='f8q6is$tg3$1@sea.gmane.org' \
--to=bschubert@q-leap.de \
--cc=linux-ext4@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.