avoid using atoi
Akim Demaille <[email protected]> Sat, 25 Sep 2021 11:50:54 +0200
| Newsgroups | gmane.comp.parsers.bison.patches |
|---|---|
| Message-ID | <[email protected]> |
Part of 3.8.2. commit 028df02a9fd60a489caaed066e5b07f96e029878 Author: Akim Demaille <[email protected]> Date: Sat Sep 25 07:32:33 2021 +0200 avoid using atoi =20 * cfg.mk: Disable sc_indent as auto indent is too invasive for now. Enable sc_prohibit_atoi_atof, except where we don't care. * src/location.c, src/muscle-tab.c: Use strtol instead of atoi. diff --git a/cfg.mk b/cfg.mk index 8fb39a8a..9c3f334d 100644 --- a/cfg.mk +++ b/cfg.mk @@ -42,7 +42,7 @@ url_dir_list =3D \ # Tests not to run as part of "make distcheck". local-checks-to-skip =3D \ sc_immutable_NEWS \ - sc_prohibit_atoi_atof + sc_indent =20 # The local directory containing the checked-out copy of gnulib used in # this release. Used solely to get a date for the "announcement" = target. @@ -164,6 +164,7 @@ $(call exclude, prohibit_always-defined_macros=3D^data/skeletons/yacc.c$$ = \ prohibit_always-defined_macros+=3D?|^src/(parse-gram.c|system.h)$$ = \ prohibit_always-defined_macros+=3D?|^tests/regression.at$$ = \ + prohibit_atoi_atof=3D^(doc|etc|examples|tests)/ = \ prohibit_doubled_word=3D^tests/named-refs.at$$ = \ prohibit_magic_number_exit=3D^doc/bison.texi$$ = \ prohibit_magic_number_exit+=3D?|^tests/(conflicts|regression).at$$ = \ diff --git a/src/location.c b/src/location.c index 356f4947..5edce82c 100644 --- a/src/location.c +++ b/src/location.c @@ -512,22 +512,28 @@ location_empty (location loc) && !loc.end.file && !loc.end.line && !loc.end.column; } =20 +static inline int +str_to_int (const char *s) +{ + long l =3D strtol (s, NULL, 10); + return l < 0 ? -1 : l <=3D INT_MAX ? l : INT_MAX; +} + void boundary_set_from_string (boundary *bound, char *str) { - /* Must search in reverse since the file name field may contain '.' - or ':'. */ + /* Search backwards: the file name may contain '.' or ':'. */ char *at =3D strrchr (str, '@'); if (at) { *at =3D '\0'; - bound->byte =3D atoi (at+1); + bound->byte =3D str_to_int (at + 1); } { char *dot =3D strrchr (str, '.'); aver (dot); *dot =3D '\0'; - bound->column =3D atoi (dot+1); + bound->column =3D str_to_int (dot + 1); if (!at) bound->byte =3D bound->column; } @@ -535,7 +541,7 @@ boundary_set_from_string (boundary *bound, char = *str) char *colon =3D strrchr (str, ':'); aver (colon); *colon =3D '\0'; - bound->line =3D atoi (colon+1); + bound->line =3D str_to_int (colon + 1); } bound->file =3D uniqstr_new (str); } diff --git a/src/location.h b/src/location.h index af21c70f..6e356b81 100644 --- a/src/location.h +++ b/src/location.h @@ -146,8 +146,9 @@ location_cmp (location a, location b) /* Whether this is the empty location. */ bool location_empty (location loc); =20 -/* STR must be formatted as 'file:line.column@byte' or = 'file:line.column', - it will be modified. */ +/* STR must be formatted as 'file:line.column@byte' or = 'file:line.column'. + It may be '<command line>:3.-1@-1', with -1 to denote = no-column/no-byte. + STR will be modified. */ void boundary_set_from_string (boundary *bound, char *str); =20 #endif /* ! defined LOCATION_H_ */ diff --git a/src/muscle-tab.c b/src/muscle-tab.c index 0654a3f1..3d3baf13 100644 --- a/src/muscle-tab.c +++ b/src/muscle-tab.c @@ -517,8 +517,9 @@ muscle_percent_define_insert (char const *var, = location variable_loc, char const *current_value =3D muscle_find_const (name); if (current_value) { + long l =3D strtol (muscle_find_const (how_name), NULL, 10); muscle_percent_define_how how_old - =3D atoi (muscle_find_const (how_name)); + =3D 0 <=3D l && l <=3D INT_MAX ? l : INT_MAX; if (how_old =3D=3D MUSCLE_PERCENT_DEFINE_F) goto end; /* If assigning the same value, make it a warning. */