e2fsck/e2fsprogs: use sscanf() instead of atoi() in the option parser
PATCH 1/1 e2fsck/e2fsprogs: use sscanf() instead of atoi() in the option parser
[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
e2fsck/unix.c +31 -6
@@ -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 *argswitch (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 *argputenv (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 ;
Quoted 5 lines · Bernd Schubert
[resent since the first mail doesn't seem to make it to this list]
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
Hi Bernd,
Can you please include a DCO. i.e.,
In your patch submissions. See the SUBMITTING-PATCHES file in the top-level directory of e2fsprogs source tree for the significance of the Signed-off-by line. For this patch, can you indicate whether you are willing to add a Signed-off-by: DCO?
Thanks, regards,
- Ted
Hello Ted,
Quoted 4 lines
In your patch submissions. See the SUBMITTING-PATCHES file in the top-level directory of e2fsprogs source tree for the significance of the Signed-off-by line. For this patch, can you indicate whether you are willing to add a Signed-off-by: DCO?
sorry that I did not read that file and so didn't know a signed-off is required.
Sure I'm willing to add a signed-off :) I'm also adding the patch again to this mail.
Cheers,
Bernd
Signature · 2 lines
Bernd Schubert Q-Leap Networks GmbH
e2fsck/unix.c +31 -6
@@ -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 *argswitch (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 *argputenv (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 ;