Skip to content

Commit

Permalink
Use strtol() wrappers for most atoi() calls
Browse files Browse the repository at this point in the history
  • Loading branch information
Rocco Rutte committed Jun 1, 2009
1 parent 4a4b8b6 commit 637e988
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 46 deletions.
3 changes: 1 addition & 2 deletions curs_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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++)
Expand Down
3 changes: 1 addition & 2 deletions edit.c
Original file line number Diff line number Diff line change
Expand Up @@ -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--;

Expand Down
13 changes: 7 additions & 6 deletions init.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 != '=')
{
Expand All @@ -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)
Expand Down
6 changes: 5 additions & 1 deletion main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
4 changes: 2 additions & 2 deletions menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
42 changes: 29 additions & 13 deletions mh.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)))
{
Expand All @@ -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)
Expand All @@ -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;
}

Expand Down Expand Up @@ -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);
}
Expand All @@ -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;
}

Expand Down Expand Up @@ -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);

Expand Down
16 changes: 7 additions & 9 deletions parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand All @@ -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)
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down
10 changes: 2 additions & 8 deletions resize.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion score.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
6 changes: 4 additions & 2 deletions url.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit 637e988

Please sign in to comment.