Skip to content

Commit 73710c6

Browse files
chore: replace unsafe string-to-number conversions (#103)
* chore: replace unsafe string-to-number conversions * modify pgxalib.cpp
1 parent 8df91cd commit 73710c6

14 files changed

+121
-113
lines changed

connection.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,7 @@ CC_set_translation(ConnectionClass *self)
778778
if (self->connInfo.translation_dll[0] == 0)
779779
return TRUE;
780780

781-
self->translation_option = atoi(self->connInfo.translation_option);
781+
self->translation_option = pg_atoi(self->connInfo.translation_option);
782782
self->translation_handle = LoadLibrary(self->connInfo.translation_dll);
783783

784784
if (self->translation_handle == NULL)
@@ -2082,7 +2082,7 @@ MYLOG(DETAIL_LOG_LEVEL, "Discarded a RELEASE result\n");
20822082
{
20832083
ptr = strrchr(cmdbuffer, ' ');
20842084
if (ptr)
2085-
res->recent_processed_row_count = atoi(ptr + 1);
2085+
res->recent_processed_row_count = pg_atoi(ptr + 1);
20862086
else
20872087
res->recent_processed_row_count = -1;
20882088
if (self->current_schema_valid &&

connection.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -222,11 +222,11 @@ do { \
222222
* It must be a decimal constant of the form %d.%d .
223223
*/
224224
#define PG_VERSION_GT(conn, ver) \
225-
(SERVER_VERSION_GT(conn, (int) ver, atoi(STRING_AFTER_DOT(ver))))
225+
(SERVER_VERSION_GT(conn, (int) ver, pg_atoi(STRING_AFTER_DOT(ver))))
226226
#define PG_VERSION_GE(conn, ver) \
227-
(SERVER_VERSION_GE(conn, (int) ver, atoi(STRING_AFTER_DOT(ver))))
227+
(SERVER_VERSION_GE(conn, (int) ver, pg_atoi(STRING_AFTER_DOT(ver))))
228228
#define PG_VERSION_EQ(conn, ver) \
229-
(SERVER_VERSION_EQ(conn, (int) ver, atoi(STRING_AFTER_DOT(ver))))
229+
(SERVER_VERSION_EQ(conn, (int) ver, pg_atoi(STRING_AFTER_DOT(ver))))
230230
#define PG_VERSION_LE(conn, ver) (! PG_VERSION_GT(conn, ver))
231231
#define PG_VERSION_LT(conn, ver) (! PG_VERSION_GE(conn, ver))
232232

convert.c

+21-21
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ static SQLLEN pg_bin2whex(const char *src, SQLWCHAR *dst, SQLLEN length);
235235
#elif defined(HAVE_STRTOUL)
236236
#define ATOI32U(val) strtoul(val, NULL, 10)
237237
#else /* HAVE_STRTOUL */
238-
#define ATOI32U atol
238+
#define ATOI32U(val) strtol(val, NULL, 10)
239239
#endif /* WIN32 */
240240

241241
/*
@@ -334,23 +334,23 @@ timestamp2stime(const char *str, SIMPLE_TIME *st, BOOL *bZone, int *zone)
334334
{
335335
case '+':
336336
*bZone = TRUE;
337-
*zone = atoi(&rest[1]);
337+
*zone = pg_atoi(&rest[1]);
338338
break;
339339
case '-':
340340
*bZone = TRUE;
341-
*zone = -atoi(&rest[1]);
341+
*zone = -pg_atoi(&rest[1]);
342342
break;
343343
case '.':
344344
if ((ptr = strchr(rest, '+')) != NULL)
345345
{
346346
*bZone = TRUE;
347-
*zone = atoi(&ptr[1]);
347+
*zone = pg_atoi(&ptr[1]);
348348
*ptr = '\0';
349349
}
350350
else if ((ptr = strchr(rest, '-')) != NULL)
351351
{
352352
*bZone = TRUE;
353-
*zone = -atoi(&ptr[1]);
353+
*zone = -pg_atoi(&ptr[1]);
354354
*ptr = '\0';
355355
}
356356
for (i = 1; i < 10; i++)
@@ -361,7 +361,7 @@ timestamp2stime(const char *str, SIMPLE_TIME *st, BOOL *bZone, int *zone)
361361
for (; i < 10; i++)
362362
rest[i] = '0';
363363
rest[i] = '\0';
364-
st->fr = atoi(&rest[1]);
364+
st->fr = pg_atoi(&rest[1]);
365365
break;
366366
case 'B':
367367
if (stricmp(rest, "BC") == 0)
@@ -563,7 +563,7 @@ static int getPrecisionPart(int precision, const char * precPart)
563563
memcpy(fraction, precPart, cpys);
564564
fraction[precision] = '\0';
565565

566-
return atoi(fraction);
566+
return pg_atoi(fraction);
567567
}
568568

569569
static BOOL
@@ -849,7 +849,7 @@ static double get_double_value(const char *str)
849849
#else
850850
return (double) -(HUGE_VAL * HUGE_VAL);
851851
#endif /* INFINITY */
852-
return atof(str);
852+
return pg_atof(str);
853853
}
854854

855855
static int char2guid(const char *str, SQLGUID *g)
@@ -1802,29 +1802,29 @@ MYLOG(DETAIL_LOG_LEVEL, "2stime fr=%d\n", std_time.fr);
18021802
case SQL_C_BIT:
18031803
len = 1;
18041804
if (bind_size > 0)
1805-
*((UCHAR *) rgbValueBindRow) = atoi(neut_str);
1805+
*((UCHAR *) rgbValueBindRow) = pg_atoi(neut_str);
18061806
else
1807-
*((UCHAR *) rgbValue + bind_row) = atoi(neut_str);
1807+
*((UCHAR *) rgbValue + bind_row) = pg_atoi(neut_str);
18081808

18091809
MYLOG(99, "SQL_C_BIT: bind_row = " FORMAT_POSIROW " val = %d, cb = " FORMAT_LEN ", rgb=%d\n",
1810-
bind_row, atoi(neut_str), cbValueMax, *((UCHAR *)rgbValue));
1810+
bind_row, pg_atoi(neut_str), cbValueMax, *((UCHAR *)rgbValue));
18111811
break;
18121812

18131813
case SQL_C_STINYINT:
18141814
case SQL_C_TINYINT:
18151815
len = 1;
18161816
if (bind_size > 0)
1817-
*((SCHAR *) rgbValueBindRow) = atoi(neut_str);
1817+
*((SCHAR *) rgbValueBindRow) = pg_atoi(neut_str);
18181818
else
1819-
*((SCHAR *) rgbValue + bind_row) = atoi(neut_str);
1819+
*((SCHAR *) rgbValue + bind_row) = pg_atoi(neut_str);
18201820
break;
18211821

18221822
case SQL_C_UTINYINT:
18231823
len = 1;
18241824
if (bind_size > 0)
1825-
*((UCHAR *) rgbValueBindRow) = atoi(neut_str);
1825+
*((UCHAR *) rgbValueBindRow) = pg_atoi(neut_str);
18261826
else
1827-
*((UCHAR *) rgbValue + bind_row) = atoi(neut_str);
1827+
*((UCHAR *) rgbValue + bind_row) = pg_atoi(neut_str);
18281828
break;
18291829

18301830
case SQL_C_FLOAT:
@@ -1865,26 +1865,26 @@ MYLOG(DETAIL_LOG_LEVEL, "2stime fr=%d\n", std_time.fr);
18651865
case SQL_C_SHORT:
18661866
len = 2;
18671867
if (bind_size > 0)
1868-
*((SQLSMALLINT *) rgbValueBindRow) = atoi(neut_str);
1868+
*((SQLSMALLINT *) rgbValueBindRow) = pg_atoi(neut_str);
18691869
else
1870-
*((SQLSMALLINT *) rgbValue + bind_row) = atoi(neut_str);
1870+
*((SQLSMALLINT *) rgbValue + bind_row) = pg_atoi(neut_str);
18711871
break;
18721872

18731873
case SQL_C_USHORT:
18741874
len = 2;
18751875
if (bind_size > 0)
1876-
*((SQLUSMALLINT *) rgbValueBindRow) = atoi(neut_str);
1876+
*((SQLUSMALLINT *) rgbValueBindRow) = pg_atoi(neut_str);
18771877
else
1878-
*((SQLUSMALLINT *) rgbValue + bind_row) = atoi(neut_str);
1878+
*((SQLUSMALLINT *) rgbValue + bind_row) = pg_atoi(neut_str);
18791879
break;
18801880

18811881
case SQL_C_SLONG:
18821882
case SQL_C_LONG:
18831883
len = 4;
18841884
if (bind_size > 0)
1885-
*((SQLINTEGER *) rgbValueBindRow) = atol(neut_str);
1885+
*((SQLINTEGER *) rgbValueBindRow) = pg_atol(neut_str);
18861886
else
1887-
*((SQLINTEGER *) rgbValue + bind_row) = atol(neut_str);
1887+
*((SQLINTEGER *) rgbValue + bind_row) = pg_atol(neut_str);
18881888
break;
18891889

18901890
case SQL_C_ULONG:

0 commit comments

Comments
 (0)