From: Rocco Rutte Date: Mon, 1 Jun 2009 09:26:37 +0000 (+0200) Subject: Use strtol() wrappers for most atoi() calls X-Git-Tag: neomutt-20160307~615 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=637e988f6e6b043cc81ae20f33f300f5c6c8a36d;p=neomutt Use strtol() wrappers for most atoi() calls --- diff --git a/curs_main.c b/curs_main.c index b898298a1..5c6ee3707 100644 --- a/curs_main.c +++ b/curs_main.c @@ -750,13 +750,12 @@ int mutt_index_menu (void) || !buf[0]) break; - if (! isdigit ((unsigned char) buf[0])) + if (mutt_atoi (buf, &i) < 0) { mutt_error _("Argument must be a message number."); break; } - i = atoi (buf); if (i > 0 && i <= Context->msgcount) { for (j = i-1; j < Context->msgcount; j++) diff --git a/edit.c b/edit.c index 35e45e47b..4d74912d6 100644 --- a/edit.c +++ b/edit.c @@ -153,8 +153,7 @@ be_include_messages (char *msg, char **buf, int *bufmax, int *buflen, while ((msg = strtok (msg, " ,")) != NULL) { - n = atoi (msg); - if (n > 0 && n <= Context->msgcount) + if (mutt_atoi (msg, &n) == 0 && n > 0 && n <= Context->msgcount) { n--; diff --git a/init.c b/init.c index 6cd7f6448..d6980c0be 100644 --- a/init.c +++ b/init.c @@ -2069,8 +2069,8 @@ static int parse_set (BUFFER *tmp, BUFFER *s, unsigned long data, BUFFER *err) else if (DTYPE(MuttVars[idx].type) == DT_NUM) { short *ptr = (short *) MuttVars[idx].data; - int val; - char *t; + short val; + int rc; if (query || *s->dptr != '=') { @@ -2088,16 +2088,17 @@ static int parse_set (BUFFER *tmp, BUFFER *s, unsigned long data, BUFFER *err) s->dptr++; mutt_extract_token (tmp, s, 0); - val = strtol (tmp->data, &t, 0); + rc = mutt_atos (tmp->data, (short *) &val); - if (!*tmp->data || *t || (short) val != val) + if (rc < 0 || !*tmp->data) { - snprintf (err->data, err->dsize, _("%s: invalid value"), tmp->data); + snprintf (err->data, err->dsize, _("%s: invalid value (%s)"), tmp->data, + rc == -1 ? _("format error") : _("number overflow")); r = -1; break; } else - *ptr = (short) val; + *ptr = val; /* these ones need a sanity check */ if (mutt_strcmp (MuttVars[idx].option, "history") == 0) diff --git a/main.c b/main.c index d4023ed38..baea582e2 100644 --- a/main.c +++ b/main.c @@ -631,7 +631,11 @@ int main (int argc, char **argv) case 'd': #ifdef DEBUG - debuglevel = atoi (optarg); + if (mutt_atoi (optarg, &debuglevel) < 0 || debuglevel <= 0) + { + fprintf (stderr, _("Error: value '%s' is invalid for -d.\n"), optarg); + return 1; + } printf (_("Debugging at level %d.\n"), debuglevel); #else printf _("DEBUG was not defined during compilation. Ignored.\n"); diff --git a/menu.c b/menu.c index b562891e9..21f3e5397 100644 --- a/menu.c +++ b/menu.c @@ -422,9 +422,9 @@ void menu_jump (MUTTMENU *menu) buf[0] = 0; if (mutt_get_field (_("Jump to: "), buf, sizeof (buf), 0) == 0 && buf[0]) { - n = atoi (buf) - 1; - if (n >= 0 && n < menu->max) + if (mutt_atoi (buf, &n) == 0 && n > 0 && n < menu->max + 1) { + n--; /* msg numbers are 0-based */ menu->current = n; menu->redraw = REDRAW_MOTION; } diff --git a/mh.c b/mh.c index c696e733b..42b188378 100644 --- a/mh.c +++ b/mh.c @@ -140,20 +140,25 @@ static short mhs_unset (struct mh_sequences *mhs, int i, short f) #endif -static void mh_read_token (char *t, int *first, int *last) +static int mh_read_token (char *t, int *first, int *last) { char *p; if ((p = strchr (t, '-'))) { *p++ = '\0'; - *first = atoi (t); - *last = atoi (p); + if (mutt_atoi (t, first) < 0 || mutt_atoi (t, last) < 0) + return -1; } else - *first = *last = atoi (t); + { + if (mutt_atoi (t, first) < 0) + return -1; + *last = *first; + } + return 0; } -static void mh_read_sequences (struct mh_sequences *mhs, const char *path) +static int mh_read_sequences (struct mh_sequences *mhs, const char *path) { FILE *fp; int line = 1; @@ -162,13 +167,13 @@ static void mh_read_sequences (struct mh_sequences *mhs, const char *path) size_t sz = 0; short f; - int first, last; + int first, last, rc; char pathname[_POSIX_PATH_MAX]; snprintf (pathname, sizeof (pathname), "%s/.mh_sequences", path); if (!(fp = fopen (pathname, "r"))) - return; + return 0; /* yes, ask callers to silently ignore the error */ while ((buff = mutt_read_line (buff, &sz, fp, &line, 0))) { @@ -186,14 +191,23 @@ static void mh_read_sequences (struct mh_sequences *mhs, const char *path) while ((t = strtok (NULL, " \t:"))) { - mh_read_token (t, &first, &last); + if (mh_read_token (t, &first, &last) < 0) + { + mhs_free_sequences (mhs); + rc = -1; + goto out; + } for (; first <= last; first++) mhs_set (mhs, first, f); } } + rc = 0; + +out: FREE (&buff); safe_fclose (&fp); + return 0; } static inline mode_t mh_umask (CONTEXT* ctx) @@ -219,11 +233,11 @@ int mh_buffy (const char *path) struct mh_sequences mhs; memset (&mhs, 0, sizeof (mhs)); - mh_read_sequences (&mhs, path); + if (mh_read_sequences (&mhs, path) < 0) + return 0; for (i = 0; !r && i <= mhs.max; i++) if (mhs_check (&mhs, i) & MH_SEQ_UNSEEN) r = 1; - mhs_free_sequences (&mhs); return r; } @@ -1139,7 +1153,8 @@ int mh_read_dir (CONTEXT * ctx, const char *subdir) if (ctx->magic == M_MH) { - mh_read_sequences (&mhs, ctx->path); + if (mh_read_sequences (&mhs, ctx->path) >= 0) + return -1; mh_update_maildir (md, &mhs); mhs_free_sequences (&mhs); } @@ -1148,7 +1163,7 @@ int mh_read_dir (CONTEXT * ctx, const char *subdir) if (!data->mh_umask) data->mh_umask = mh_umask (ctx); - + return 0; } @@ -2026,7 +2041,8 @@ int mh_check_mailbox (CONTEXT * ctx, int *index_hint) maildir_parse_dir (ctx, &last, NULL, NULL, NULL); maildir_delayed_parsing (ctx, &md, NULL); - mh_read_sequences (&mhs, ctx->path); + if (mh_read_sequences (&mhs, ctx->path) < 0) + return -1; mh_update_maildir (md, &mhs); mhs_free_sequences (&mhs); diff --git a/parse.c b/parse.c index 143a62e92..cba966229 100644 --- a/parse.c +++ b/parse.c @@ -614,8 +614,8 @@ BODY *mutt_parse_multipart (FILE *fp, const char *boundary, LOFF_T end_off, int #ifdef SUN_ATTACHMENT if (mutt_get_parameter ("content-lines", new->parameter)) { - for (lines = atoi(mutt_get_parameter ("content-lines", new->parameter)); - lines; lines-- ) + mutt_atoi (mutt_get_parameter ("content-lines", new->parameter), &lines); + for ( ; lines; lines-- ) if (ftello (fp) >= end_off || fgets (buffer, LONG_STRING, fp) == NULL) break; } @@ -773,9 +773,8 @@ time_t mutt_parse_date (const char *s, HEADER *h) switch (count) { case 0: /* day of the month */ - if (!isdigit ((unsigned char) *t)) + if (mutt_atoi (t, &tm.tm_mday) < 0 || tm.tm_mday < 0) return (-1); - tm.tm_mday = atoi (t); if (tm.tm_mday > 31) return (-1); break; @@ -787,7 +786,8 @@ time_t mutt_parse_date (const char *s, HEADER *h) break; case 2: /* year */ - tm.tm_year = atoi (t); + if (mutt_atoi (t, &tm.tm_year) < 0 || tm.tm_year < 0) + return (-1); if (tm.tm_year < 50) tm.tm_year += 100; else if (tm.tm_year >= 1900) @@ -1022,7 +1022,7 @@ int mutt_parse_rfc822_line (ENVELOPE *e, HEADER *hdr, char *line, char *p, short { if (hdr) { - if ((hdr->content->length = atoi (p)) < 0) + if ((hdr->content->length = atol (p)) < 0) hdr->content->length = -1; } matched = 1; @@ -1083,13 +1083,11 @@ int mutt_parse_rfc822_line (ENVELOPE *e, HEADER *hdr, char *line, char *p, short { if (hdr) { - hdr->lines = atoi (p); - /* * HACK - mutt has, for a very short time, produced negative * Lines header values. Ignore them. */ - if (hdr->lines < 0) + if (mutt_atoi (p, &hdr->lines) < 0 || hdr->lines < 0) hdr->lines = 0; } diff --git a/resize.c b/resize.c index 79b01accc..f9c26b427 100644 --- a/resize.c +++ b/resize.c @@ -59,18 +59,12 @@ void mutt_resize_screen (void) } if (SLtt_Screen_Rows <= 0) { - if ((cp = getenv ("LINES")) != NULL) - { - SLtt_Screen_Rows = atoi (cp); - } - else + if ((cp = getenv ("LINES")) != NULL && mutt_atoi (cp, &SLtt_Screen_Rows) < 0) SLtt_Screen_Rows = 24; } if (SLtt_Screen_Cols <= 0) { - if ((cp = getenv ("COLUMNS")) != NULL) - SLtt_Screen_Cols = atoi (cp); - else + if ((cp = getenv ("COLUMNS")) != NULL && mutt_atoi (cp, &SLtt_Screen_Cols) < 0) SLtt_Screen_Cols = 80; } #ifdef USE_SLANG_CURSES diff --git a/score.c b/score.c index 5e8dfe190..b8a1f6cd6 100644 --- a/score.c +++ b/score.c @@ -116,7 +116,12 @@ int mutt_parse_score (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err) ptr->exact = 1; pc++; } - ptr->val = atoi (pc); + if (mutt_atoi (pc, &ptr->val) < 0) + { + FREE (&pattern); + strfcpy (err->data, _("Error: score: invalid number"), err->dsize); + return (-1); + } set_option (OPTNEEDRESCORE); return 0; } diff --git a/url.c b/url.c index 5155371d3..58c364213 100644 --- a/url.c +++ b/url.c @@ -144,7 +144,8 @@ static char *ciss_parse_userhost (ciss_url_t *ciss, char *src) if ((p = strchr (t, ':'))) { *p++ = '\0'; - ciss->port = atoi (p); + if (mutt_atos (p, (short*) &ciss->port) < 0) + return NULL; } else ciss->port = 0; @@ -165,7 +166,8 @@ int url_parse_ciss (ciss_url_t *ciss, char *src) tmp = strchr (src, ':') + 1; - ciss->path = ciss_parse_userhost (ciss, tmp); + if ((ciss->path = ciss_parse_userhost (ciss, tmp)) == NULL) + return -1; url_pct_decode (ciss->path); return 0;