Skip to content

Commit 7350847

Browse files
committed
Remove pg_atoi()
The last caller was int2vectorin(), and having such a general function for one user didn't seem useful, so just put the required parts inline and remove the function. Reviewed-by: John Naylor <john.naylor@enterprisedb.com> Discussion: https://www.postgresql.org/message-id/flat/b239564c-cad0-b23e-c57e-166d883cb97d@enterprisedb.com
1 parent 278cdea commit 7350847

File tree

3 files changed

+28
-93
lines changed

3 files changed

+28
-93
lines changed

src/backend/utils/adt/int.c

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,15 +146,39 @@ int2vectorin(PG_FUNCTION_ARGS)
146146

147147
result = (int2vector *) palloc0(Int2VectorSize(FUNC_MAX_ARGS));
148148

149-
for (n = 0; *intString && n < FUNC_MAX_ARGS; n++)
149+
for (n = 0; n < FUNC_MAX_ARGS; n++)
150150
{
151+
long l;
152+
char *endp;
153+
151154
while (*intString && isspace((unsigned char) *intString))
152155
intString++;
153156
if (*intString == '\0')
154157
break;
155-
result->values[n] = pg_atoi(intString, sizeof(int16), ' ');
156-
while (*intString && !isspace((unsigned char) *intString))
157-
intString++;
158+
159+
errno = 0;
160+
l = strtol(intString, &endp, 10);
161+
162+
if (intString == endp)
163+
ereport(ERROR,
164+
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
165+
errmsg("invalid input syntax for type %s: \"%s\"",
166+
"smallint", intString)));
167+
168+
if (errno == ERANGE || l < SHRT_MIN || l > SHRT_MAX)
169+
ereport(ERROR,
170+
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
171+
errmsg("value \"%s\" is out of range for type %s", intString,
172+
"smallint")));
173+
174+
if (*endp && *endp != ' ')
175+
ereport(ERROR,
176+
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
177+
errmsg("invalid input syntax for type %s: \"%s\"",
178+
"integer", intString)));
179+
180+
result->values[n] = l;
181+
intString = endp;
158182
}
159183
while (*intString && isspace((unsigned char) *intString))
160184
intString++;

src/backend/utils/adt/numutils.c

Lines changed: 0 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -85,94 +85,6 @@ decimalLength64(const uint64 v)
8585
return t + (v >= PowersOfTen[t]);
8686
}
8787

88-
/*
89-
* pg_atoi: convert string to integer
90-
*
91-
* allows any number of leading or trailing whitespace characters.
92-
*
93-
* 'size' is the sizeof() the desired integral result (1, 2, or 4 bytes).
94-
*
95-
* c, if not 0, is a terminator character that may appear after the
96-
* integer (plus whitespace). If 0, the string must end after the integer.
97-
*
98-
* Unlike plain atoi(), this will throw ereport() upon bad input format or
99-
* overflow.
100-
*/
101-
int32
102-
pg_atoi(const char *s, int size, int c)
103-
{
104-
long l;
105-
char *badp;
106-
107-
/*
108-
* Some versions of strtol treat the empty string as an error, but some
109-
* seem not to. Make an explicit test to be sure we catch it.
110-
*/
111-
if (s == NULL)
112-
elog(ERROR, "NULL pointer");
113-
if (*s == 0)
114-
ereport(ERROR,
115-
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
116-
errmsg("invalid input syntax for type %s: \"%s\"",
117-
"integer", s)));
118-
119-
errno = 0;
120-
l = strtol(s, &badp, 10);
121-
122-
/* We made no progress parsing the string, so bail out */
123-
if (s == badp)
124-
ereport(ERROR,
125-
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
126-
errmsg("invalid input syntax for type %s: \"%s\"",
127-
"integer", s)));
128-
129-
switch (size)
130-
{
131-
case sizeof(int32):
132-
if (errno == ERANGE
133-
#if defined(HAVE_LONG_INT_64)
134-
/* won't get ERANGE on these with 64-bit longs... */
135-
|| l < INT_MIN || l > INT_MAX
136-
#endif
137-
)
138-
ereport(ERROR,
139-
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
140-
errmsg("value \"%s\" is out of range for type %s", s,
141-
"integer")));
142-
break;
143-
case sizeof(int16):
144-
if (errno == ERANGE || l < SHRT_MIN || l > SHRT_MAX)
145-
ereport(ERROR,
146-
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
147-
errmsg("value \"%s\" is out of range for type %s", s,
148-
"smallint")));
149-
break;
150-
case sizeof(int8):
151-
if (errno == ERANGE || l < SCHAR_MIN || l > SCHAR_MAX)
152-
ereport(ERROR,
153-
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
154-
errmsg("value \"%s\" is out of range for 8-bit integer", s)));
155-
break;
156-
default:
157-
elog(ERROR, "unsupported result size: %d", size);
158-
}
159-
160-
/*
161-
* Skip any trailing whitespace; if anything but whitespace remains before
162-
* the terminating character, bail out
163-
*/
164-
while (*badp && *badp != c && isspace((unsigned char) *badp))
165-
badp++;
166-
167-
if (*badp && *badp != c)
168-
ereport(ERROR,
169-
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
170-
errmsg("invalid input syntax for type %s: \"%s\"",
171-
"integer", s)));
172-
173-
return (int32) l;
174-
}
175-
17688
/*
17789
* Convert input string to a signed 16 bit integer.
17890
*

src/include/utils/builtins.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ extern void namestrcpy(Name name, const char *str);
4343
extern int namestrcmp(Name name, const char *str);
4444

4545
/* numutils.c */
46-
extern int32 pg_atoi(const char *s, int size, int c);
4746
extern int16 pg_strtoint16(const char *s);
4847
extern int32 pg_strtoint32(const char *s);
4948
extern int64 pg_strtoint64(const char *s);

0 commit comments

Comments
 (0)