Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 2e82d0b

Browse files
committed
Prevent datebsearch() from crashing on base == NULL && nel == 0.
Normally nel == 0 works okay because the initial value of "last" will be less than "base"; but if "base" is zero then the calculation wraps around and we have a very large (unsigned) value for "last", so that the loop can be entered and we get a SIGSEGV on a bogus pointer. This is certainly the proximate cause of the recent reports of Windows builds crashing on 'infinity'::timestamp --- evidently, they're either not setting an active timezonetktbl, or setting an empty one. It's not yet clear to me why it's only happening on Windows and not happening on any buildfarm member. But even if that's due to some bug elsewhere, it seems wise for this function to not choke on the powerup values of timezonetktbl/sztimezonetktbl. I also changed the copy of this code in ecpglib, although I am not sure whether it's exposed to a similar hazard. Per report and stack trace from Richard Broersma.
1 parent 7ff7711 commit 2e82d0b

File tree

2 files changed

+34
-28
lines changed

2 files changed

+34
-28
lines changed

src/backend/utils/adt/datetime.c

+17-14
Original file line numberDiff line numberDiff line change
@@ -3569,24 +3569,27 @@ DateTimeParseError(int dterr, const char *str, const char *datatype)
35693569
static const datetkn *
35703570
datebsearch(const char *key, const datetkn *base, int nel)
35713571
{
3572-
const datetkn *last = base + nel - 1,
3573-
*position;
3574-
int result;
3575-
3576-
while (last >= base)
3572+
if (nel > 0)
35773573
{
3578-
position = base + ((last - base) >> 1);
3579-
result = key[0] - position->token[0];
3580-
if (result == 0)
3574+
const datetkn *last = base + nel - 1,
3575+
*position;
3576+
int result;
3577+
3578+
while (last >= base)
35813579
{
3582-
result = strncmp(key, position->token, TOKMAXLEN);
3580+
position = base + ((last - base) >> 1);
3581+
result = key[0] - position->token[0];
35833582
if (result == 0)
3584-
return position;
3583+
{
3584+
result = strncmp(key, position->token, TOKMAXLEN);
3585+
if (result == 0)
3586+
return position;
3587+
}
3588+
if (result < 0)
3589+
last = position - 1;
3590+
else
3591+
base = position + 1;
35853592
}
3586-
if (result < 0)
3587-
last = position - 1;
3588-
else
3589-
base = position + 1;
35903593
}
35913594
return NULL;
35923595
}

src/interfaces/ecpg/pgtypeslib/dt_common.c

+17-14
Original file line numberDiff line numberDiff line change
@@ -512,24 +512,27 @@ char *pgtypes_date_months[] = {"January", "February", "March", "April", "May"
512512
static datetkn *
513513
datebsearch(char *key, datetkn *base, unsigned int nel)
514514
{
515-
datetkn *last = base + nel - 1,
516-
*position;
517-
int result;
518-
519-
while (last >= base)
515+
if (nel > 0)
520516
{
521-
position = base + ((last - base) >> 1);
522-
result = key[0] - position->token[0];
523-
if (result == 0)
517+
datetkn *last = base + nel - 1,
518+
*position;
519+
int result;
520+
521+
while (last >= base)
524522
{
525-
result = strncmp(key, position->token, TOKMAXLEN);
523+
position = base + ((last - base) >> 1);
524+
result = key[0] - position->token[0];
526525
if (result == 0)
527-
return position;
526+
{
527+
result = strncmp(key, position->token, TOKMAXLEN);
528+
if (result == 0)
529+
return position;
530+
}
531+
if (result < 0)
532+
last = position - 1;
533+
else
534+
base = position + 1;
528535
}
529-
if (result < 0)
530-
last = position - 1;
531-
else
532-
base = position + 1;
533536
}
534537
return NULL;
535538
}

0 commit comments

Comments
 (0)