mirror of
				https://github.com/postgres/postgres.git
				synced 2025-11-03 09:13:20 +03:00 
			
		
		
		
	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 8f427187d.
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
			
			
This commit is contained in:
		@@ -31,15 +31,18 @@ pg_strtof(const char *nptr, char **endptr)
 | 
				
			|||||||
{
 | 
					{
 | 
				
			||||||
	int			caller_errno = errno;
 | 
						int			caller_errno = errno;
 | 
				
			||||||
	float		fresult;
 | 
						float		fresult;
 | 
				
			||||||
 | 
						char	   *myendptr;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	errno = 0;
 | 
						errno = 0;
 | 
				
			||||||
	fresult = (strtof) (nptr, endptr);
 | 
						fresult = (strtof) (nptr, &myendptr);
 | 
				
			||||||
 | 
						if (endptr)
 | 
				
			||||||
 | 
							*endptr = myendptr;
 | 
				
			||||||
	if (errno)
 | 
						if (errno)
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		/* On error, just return the error to the caller. */
 | 
							/* On error, just return the error to the caller. */
 | 
				
			||||||
		return fresult;
 | 
							return fresult;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	else if ((*endptr == nptr) || isnan(fresult) ||
 | 
						else if ((myendptr == nptr) || isnan(fresult) ||
 | 
				
			||||||
			 ((fresult >= FLT_MIN || fresult <= -FLT_MIN) && !isinf(fresult)))
 | 
								 ((fresult >= FLT_MIN || fresult <= -FLT_MIN) && !isinf(fresult)))
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		/*
 | 
							/*
 | 
				
			||||||
@@ -53,7 +56,8 @@ pg_strtof(const char *nptr, char **endptr)
 | 
				
			|||||||
	else
 | 
						else
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		/*
 | 
							/*
 | 
				
			||||||
		 * Try again. errno is already 0 here.
 | 
							 * Try again.  errno is already 0 here, and we assume that the endptr
 | 
				
			||||||
 | 
							 * won't be any different.
 | 
				
			||||||
		 */
 | 
							 */
 | 
				
			||||||
		double		dresult = strtod(nptr, NULL);
 | 
							double		dresult = strtod(nptr, NULL);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user