File tree Expand file tree Collapse file tree 9 files changed +39
-49
lines changed Expand file tree Collapse file tree 9 files changed +39
-49
lines changed Original file line number Diff line number Diff line change 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 }
Original file line number Diff line number Diff line change 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)
12111212static int
12121213process_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);
Original file line number Diff line number Diff line change 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};
251252static 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,
Original file line number Diff line number Diff 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+ }
Original file line number Diff line number Diff line change 1111#define COMMON_STRING_H
1212
1313extern 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 */
Original file line number Diff line number Diff line change 44/pgstrcasecmp.c
55/rint.c
66/snprintf.c
7+ /string.c
78/strnlen.c
Original file line number Diff line number Diff line change @@ -32,6 +32,7 @@ SHLIB_EXPORTS = exports.txt
3232OBJS = 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
3738all : all-lib
@@ -47,13 +48,16 @@ include $(top_srcdir)/src/Makefile.shlib
4748pgstrcasecmp.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+
5054install : all installdirs install-lib
5155
5256installdirs : installdirs-lib
5357
5458uninstall : uninstall-lib
5559
5660clean 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
5963maintainer-clean : distclean maintainer-clean-lib
Original file line number Diff line number Diff line change 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 */
Original file line number Diff line number Diff line change 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);
You can’t perform that action at this time.
0 commit comments