Skip to content

Commit 8706d41

Browse files
tglsfdcCommitfest Bot
authored andcommitted
Don't leak the startup-packet buffer in ProcessStartupPacket.
This is the first actual leakage bug fix in this patch series. The amount of memory regained is quite negligible of course. But we don't want Valgrind whining about this in every session. Author: Tom Lane <[email protected]> Discussion: https://postgr.es/m/[email protected]
1 parent 793a064 commit 8706d41

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

src/backend/tcop/backend_startup.c

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ static int
492492
ProcessStartupPacket(Port *port, bool ssl_done, bool gss_done)
493493
{
494494
int32 len;
495-
char *buf;
495+
char *buf = NULL;
496496
ProtocolVersion proto;
497497
MemoryContext oldcontext;
498498

@@ -516,7 +516,7 @@ ProcessStartupPacket(Port *port, bool ssl_done, bool gss_done)
516516
* scanners, which may be less benign, but it's not really our job to
517517
* notice those.)
518518
*/
519-
return STATUS_ERROR;
519+
goto fail;
520520
}
521521

522522
if (pq_getbytes(((char *) &len) + 1, 3) == EOF)
@@ -526,7 +526,7 @@ ProcessStartupPacket(Port *port, bool ssl_done, bool gss_done)
526526
ereport(COMMERROR,
527527
(errcode(ERRCODE_PROTOCOL_VIOLATION),
528528
errmsg("incomplete startup packet")));
529-
return STATUS_ERROR;
529+
goto fail;
530530
}
531531

532532
len = pg_ntoh32(len);
@@ -538,7 +538,7 @@ ProcessStartupPacket(Port *port, bool ssl_done, bool gss_done)
538538
ereport(COMMERROR,
539539
(errcode(ERRCODE_PROTOCOL_VIOLATION),
540540
errmsg("invalid length of startup packet")));
541-
return STATUS_ERROR;
541+
goto fail;
542542
}
543543

544544
/*
@@ -554,7 +554,7 @@ ProcessStartupPacket(Port *port, bool ssl_done, bool gss_done)
554554
ereport(COMMERROR,
555555
(errcode(ERRCODE_PROTOCOL_VIOLATION),
556556
errmsg("incomplete startup packet")));
557-
return STATUS_ERROR;
557+
goto fail;
558558
}
559559
pq_endmsgread();
560560

@@ -568,7 +568,7 @@ ProcessStartupPacket(Port *port, bool ssl_done, bool gss_done)
568568
{
569569
ProcessCancelRequestPacket(port, buf, len);
570570
/* Not really an error, but we don't want to proceed further */
571-
return STATUS_ERROR;
571+
goto fail;
572572
}
573573

574574
if (proto == NEGOTIATE_SSL_CODE && !ssl_done)
@@ -607,14 +607,16 @@ ProcessStartupPacket(Port *port, bool ssl_done, bool gss_done)
607607
ereport(COMMERROR,
608608
(errcode_for_socket_access(),
609609
errmsg("failed to send SSL negotiation response: %m")));
610-
return STATUS_ERROR; /* close the connection */
610+
goto fail; /* close the connection */
611611
}
612612

613613
#ifdef USE_SSL
614614
if (SSLok == 'S' && secure_open_server(port) == -1)
615-
return STATUS_ERROR;
615+
goto fail;
616616
#endif
617617

618+
pfree(buf);
619+
618620
/*
619621
* At this point we should have no data already buffered. If we do,
620622
* it was received before we performed the SSL handshake, so it wasn't
@@ -661,14 +663,16 @@ ProcessStartupPacket(Port *port, bool ssl_done, bool gss_done)
661663
ereport(COMMERROR,
662664
(errcode_for_socket_access(),
663665
errmsg("failed to send GSSAPI negotiation response: %m")));
664-
return STATUS_ERROR; /* close the connection */
666+
goto fail; /* close the connection */
665667
}
666668

667669
#ifdef ENABLE_GSS
668670
if (GSSok == 'G' && secure_open_gssapi(port) == -1)
669-
return STATUS_ERROR;
671+
goto fail;
670672
#endif
671673

674+
pfree(buf);
675+
672676
/*
673677
* At this point we should have no data already buffered. If we do,
674678
* it was received before we performed the GSS handshake, so it wasn't
@@ -863,7 +867,16 @@ ProcessStartupPacket(Port *port, bool ssl_done, bool gss_done)
863867
*/
864868
MemoryContextSwitchTo(oldcontext);
865869

870+
pfree(buf);
871+
866872
return STATUS_OK;
873+
874+
fail:
875+
/* be tidy, just to avoid Valgrind complaints */
876+
if (buf)
877+
pfree(buf);
878+
879+
return STATUS_ERROR;
867880
}
868881

869882
/*

0 commit comments

Comments
 (0)