Skip to content

Commit 17bb625

Browse files
committed
Move strtoint() to common
Several places used similar code to convert a string to an int, so take the function that we already had and make it globally available. Reviewed-by: Michael Paquier <michael@paquier.xyz>
1 parent 6cf86f4 commit 17bb625

File tree

9 files changed

+39
-49
lines changed

9 files changed

+39
-49
lines changed

src/backend/nodes/read.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include <ctype.h>
2323

24+
#include "common/string.h"
2425
#include "nodes/pg_list.h"
2526
#include "nodes/readfuncs.h"
2627
#include "nodes/value.h"
@@ -215,18 +216,15 @@ nodeTokenType(char *token, int length)
215216
{
216217
/*
217218
* Yes. Figure out whether it is integral or float; this requires
218-
* both a syntax check and a range check. strtol() can do both for us.
219-
* We know the token will end at a character that strtol will stop at,
219+
* both a syntax check and a range check. strtoint() can do both for us.
220+
* We know the token will end at a character that strtoint will stop at,
220221
* so we do not need to modify the string.
221222
*/
222-
long val;
223223
char *endptr;
224224

225225
errno = 0;
226-
val = strtol(token, &endptr, 10);
227-
if (endptr != token + length || errno == ERANGE ||
228-
/* check for overflow of int */
229-
val != (int) val)
226+
(void) strtoint(token, &endptr, 10);
227+
if (endptr != token + length || errno == ERANGE)
230228
return T_Float;
231229
return T_Integer;
232230
}

src/backend/parser/scan.l

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <ctype.h>
3535
#include <unistd.h>
3636

37+
#include "common/string.h"
3738
#include "parser/gramparse.h"
3839
#include "parser/parser.h" /* only needed for GUC variables */
3940
#include "parser/scansup.h"
@@ -1211,14 +1212,12 @@ litbufdup(core_yyscan_t yyscanner)
12111212
static int
12121213
process_integer_literal(const char *token, YYSTYPE *lval)
12131214
{
1214-
long val;
1215+
int val;
12151216
char *endptr;
12161217

12171218
errno = 0;
1218-
val = strtol(token, &endptr, 10);
1219-
if (*endptr != '\0' || errno == ERANGE ||
1220-
/* check for overflow of int */
1221-
val != (int) val)
1219+
val = strtoint(token, &endptr, 10);
1220+
if (*endptr != '\0' || errno == ERANGE)
12221221
{
12231222
/* integer too large, treat it as a float */
12241223
lval->str = pstrdup(token);

src/backend/utils/adt/datetime.c

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "access/htup_details.h"
2323
#include "access/xact.h"
2424
#include "catalog/pg_type.h"
25+
#include "common/string.h"
2526
#include "funcapi.h"
2627
#include "miscadmin.h"
2728
#include "nodes/nodeFuncs.h"
@@ -251,23 +252,6 @@ static const datetkn *deltacache[MAXDATEFIELDS] = {NULL};
251252
static const datetkn *abbrevcache[MAXDATEFIELDS] = {NULL};
252253

253254

254-
/*
255-
* strtoint --- just like strtol, but returns int not long
256-
*/
257-
static int
258-
strtoint(const char *nptr, char **endptr, int base)
259-
{
260-
long val;
261-
262-
val = strtol(nptr, endptr, base);
263-
#ifdef HAVE_LONG_INT_64
264-
if (val != (long) ((int32) val))
265-
errno = ERANGE;
266-
#endif
267-
return (int) val;
268-
}
269-
270-
271255
/*
272256
* Calendar time to Julian date conversions.
273257
* Julian date is commonly used in astronomical applications,

src/common/string.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,18 @@ pg_str_endswith(const char *str, const char *end)
4141
str += slen - elen;
4242
return strcmp(str, end) == 0;
4343
}
44+
45+
46+
/*
47+
* strtoint --- just like strtol, but returns int not long
48+
*/
49+
int
50+
strtoint(const char *restrict str, char **restrict endptr, int base)
51+
{
52+
long val;
53+
54+
val = strtol(str, endptr, base);
55+
if (val != (int) val)
56+
errno = ERANGE;
57+
return (int) val;
58+
}

src/include/common/string.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@
1111
#define COMMON_STRING_H
1212

1313
extern bool pg_str_endswith(const char *str, const char *end);
14+
extern int strtoint(const char *restrict str, char **restrict endptr, int base);
1415

1516
#endif /* COMMON_STRING_H */

src/interfaces/ecpg/pgtypeslib/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
/pgstrcasecmp.c
55
/rint.c
66
/snprintf.c
7+
/string.c
78
/strnlen.c

src/interfaces/ecpg/pgtypeslib/Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ SHLIB_EXPORTS = exports.txt
3232
OBJS= numeric.o datetime.o common.o dt_common.o timestamp.o interval.o \
3333
pgstrcasecmp.o \
3434
$(filter rint.o snprintf.o strnlen.o, $(LIBOBJS)) \
35+
string.o \
3536
$(WIN32RES)
3637

3738
all: all-lib
@@ -47,13 +48,16 @@ include $(top_srcdir)/src/Makefile.shlib
4748
pgstrcasecmp.c rint.c snprintf.c strnlen.c: % : $(top_srcdir)/src/port/%
4849
rm -f $@ && $(LN_S) $< .
4950

51+
string.c: % : $(top_srcdir)/src/common/%
52+
rm -f $@ && $(LN_S) $< .
53+
5054
install: all installdirs install-lib
5155

5256
installdirs: installdirs-lib
5357

5458
uninstall: uninstall-lib
5559

5660
clean distclean: clean-lib
57-
rm -f $(OBJS) pgstrcasecmp.c rint.c snprintf.c strnlen.c
61+
rm -f $(OBJS) pgstrcasecmp.c rint.c snprintf.c strnlen.c string.c
5862

5963
maintainer-clean: distclean maintainer-clean-lib

src/interfaces/ecpg/pgtypeslib/interval.c

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,13 @@
99
#error -ffast-math is known to break this code
1010
#endif
1111

12+
#include "common/string.h"
13+
1214
#include "extern.h"
1315
#include "dt.h"
1416
#include "pgtypes_error.h"
1517
#include "pgtypes_interval.h"
1618

17-
/* copy&pasted from .../src/backend/utils/adt/datetime.c */
18-
static int
19-
strtoint(const char *nptr, char **endptr, int base)
20-
{
21-
long val;
22-
23-
val = strtol(nptr, endptr, base);
24-
#ifdef HAVE_LONG_INT_64
25-
if (val != (long) ((int32) val))
26-
errno = ERANGE;
27-
#endif
28-
return (int) val;
29-
}
30-
3119
/* copy&pasted from .../src/backend/utils/adt/datetime.c
3220
* and changesd struct pg_tm to struct tm
3321
*/

src/interfaces/ecpg/preproc/pgc.l

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#include <ctype.h>
2222
#include <limits.h>
2323

24+
#include "common/string.h"
25+
2426
#include "extern.h"
2527
#include "preproc.h"
2628
}
@@ -727,14 +729,12 @@ cppline {space}*#([^i][A-Za-z]*|{if}|{ifdef}|{ifndef}|{import})((\/\*[^*/]*\*+
727729
return PARAM;
728730
}
729731
<C,SQL>{integer} {
730-
long val;
732+
int val;
731733
char* endptr;
732734

733735
errno = 0;
734-
val = strtol((char *)yytext, &endptr,10);
735-
if (*endptr != '\0' || errno == ERANGE ||
736-
/* check for overflow of int */
737-
val != (int) val)
736+
val = strtoint(yytext, &endptr, 10);
737+
if (*endptr != '\0' || errno == ERANGE)
738738
{
739739
errno = 0;
740740
base_yylval.str = mm_strdup(yytext);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy