Skip to content

Commit 930b785

Browse files
committed
Minor cleanup/future-proofing for pg_saslprep().
Ensure that pg_saslprep() initializes its output argument to NULL in all failure paths, and then remove the redundant initialization that some (not all) of its callers did. This does not fix any live bug, but it reduces the odds of future bugs of omission. Also add a comment about why the existing failure-path coding is adequate. Back-patch so as to keep the function's API consistent across branches, again to forestall future bug introduction. Patch by me, reviewed by Michael Paquier Discussion: https://postgr.es/m/16558.1536407783@sss.pgh.pa.us
1 parent 3985b75 commit 930b785

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-6
lines changed

src/backend/libpq/auth-scram.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ pg_be_scram_exchange(void *opaq, char *input, int inputlen,
382382
char *
383383
pg_be_scram_build_verifier(const char *password)
384384
{
385-
char *prep_password = NULL;
385+
char *prep_password;
386386
pg_saslprep_rc rc;
387387
char saltbuf[SCRAM_DEFAULT_SALT_LEN];
388388
char *result;
@@ -428,7 +428,7 @@ scram_verify_plain_password(const char *username, const char *password,
428428
uint8 stored_key[SCRAM_KEY_LEN];
429429
uint8 server_key[SCRAM_KEY_LEN];
430430
uint8 computed_key[SCRAM_KEY_LEN];
431-
char *prep_password = NULL;
431+
char *prep_password;
432432
pg_saslprep_rc rc;
433433

434434
if (!parse_scram_verifier(verifier, &iterations, &encoded_salt,

src/common/saslprep.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,6 +1081,9 @@ pg_saslprep(const char *input, char **output)
10811081
unsigned char *p;
10821082
pg_wchar *wp;
10831083

1084+
/* Ensure we return *output as NULL on failure */
1085+
*output = NULL;
1086+
10841087
/* Check that the password isn't stupendously long */
10851088
if (strlen(input) > MAX_PASSWORD_LENGTH)
10861089
{
@@ -1112,10 +1115,7 @@ pg_saslprep(const char *input, char **output)
11121115
*/
11131116
input_size = pg_utf8_string_len(input);
11141117
if (input_size < 0)
1115-
{
1116-
*output = NULL;
11171118
return SASLPREP_INVALID_UTF8;
1118-
}
11191119

11201120
input_chars = ALLOC((input_size + 1) * sizeof(pg_wchar));
11211121
if (!input_chars)
@@ -1246,6 +1246,11 @@ pg_saslprep(const char *input, char **output)
12461246
result = ALLOC(result_size + 1);
12471247
if (!result)
12481248
goto oom;
1249+
1250+
/*
1251+
* There are no error exits below here, so the error exit paths don't need
1252+
* to worry about possibly freeing "result".
1253+
*/
12491254
p = (unsigned char *) result;
12501255
for (wp = output_chars; *wp; wp++)
12511256
{

src/interfaces/libpq/fe-auth-scram.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,7 @@ verify_server_signature(fe_scram_state *state)
621621
char *
622622
pg_fe_scram_build_verifier(const char *password)
623623
{
624-
char *prep_password = NULL;
624+
char *prep_password;
625625
pg_saslprep_rc rc;
626626
char saltbuf[SCRAM_DEFAULT_SALT_LEN];
627627
char *result;

0 commit comments

Comments
 (0)