Skip to content

Commit ebe919e

Browse files
committed
Fix pg_strtof() to not crash on NULL endptr.
We had managed not to notice this simple oversight because none of our calls exercised the case --- until commit 8f42718. That led to pg_dump crashing on any platform that uses this code (currently Cygwin and Mingw). Even though there's no immediate bug in the back branches, backpatch, because a non-POSIX-compliant strtof() substitute is trouble waiting to happen for extensions or future back-patches. Diagnosed-by: Alexander Lakhin <exclusion@gmail.com> Author: Tom Lane <tgl@sss.pgh.pa.us> Discussion: https://postgr.es/m/339b3902-4e98-4e31-a744-94e43b7b9292@gmail.com Backpatch-through: 13
1 parent 5302ff9 commit ebe919e

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

src/port/strtof.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,18 @@ pg_strtof(const char *nptr, char **endptr)
7676
{
7777
int caller_errno = errno;
7878
float fresult;
79+
char *myendptr;
7980

8081
errno = 0;
81-
fresult = (strtof) (nptr, endptr);
82+
fresult = (strtof) (nptr, &myendptr);
83+
if (endptr)
84+
*endptr = myendptr;
8285
if (errno)
8386
{
8487
/* On error, just return the error to the caller. */
8588
return fresult;
8689
}
87-
else if ((*endptr == nptr) || isnan(fresult) ||
90+
else if ((myendptr == nptr) || isnan(fresult) ||
8891
((fresult >= FLT_MIN || fresult <= -FLT_MIN) && !isinf(fresult)))
8992
{
9093
/*
@@ -98,7 +101,8 @@ pg_strtof(const char *nptr, char **endptr)
98101
else
99102
{
100103
/*
101-
* Try again. errno is already 0 here.
104+
* Try again. errno is already 0 here, and we assume that the endptr
105+
* won't be any different.
102106
*/
103107
double dresult = strtod(nptr, NULL);
104108

0 commit comments

Comments
 (0)