Skip to content

Commit a9e9a9f

Browse files
committed
libpq error message refactoring, part 2
This applies the new APIs to the code. Reviewed-by: Alvaro Herrera <alvherre@alvh.no-ip.org> Discussion: https://www.postgresql.org/message-id/flat/7c0232ef-7b44-68db-599d-b327d0640a77@enterprisedb.com
1 parent 0873b2d commit a9e9a9f

12 files changed

+463
-774
lines changed

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

+31-56
Original file line numberDiff line numberDiff line change
@@ -218,14 +218,12 @@ scram_exchange(void *opaq, char *input, int inputlen,
218218
{
219219
if (inputlen == 0)
220220
{
221-
appendPQExpBufferStr(&conn->errorMessage,
222-
libpq_gettext("malformed SCRAM message (empty message)\n"));
221+
libpq_append_conn_error(conn, "malformed SCRAM message (empty message)");
223222
goto error;
224223
}
225224
if (inputlen != strlen(input))
226225
{
227-
appendPQExpBufferStr(&conn->errorMessage,
228-
libpq_gettext("malformed SCRAM message (length mismatch)\n"));
226+
libpq_append_conn_error(conn, "malformed SCRAM message (length mismatch)");
229227
goto error;
230228
}
231229
}
@@ -268,24 +266,21 @@ scram_exchange(void *opaq, char *input, int inputlen,
268266
*/
269267
if (!verify_server_signature(state, success, &errstr))
270268
{
271-
appendPQExpBuffer(&conn->errorMessage,
272-
libpq_gettext("could not verify server signature: %s\n"), errstr);
269+
libpq_append_conn_error(conn, "could not verify server signature: %s", errstr);
273270
goto error;
274271
}
275272

276273
if (!*success)
277274
{
278-
appendPQExpBufferStr(&conn->errorMessage,
279-
libpq_gettext("incorrect server signature\n"));
275+
libpq_append_conn_error(conn, "incorrect server signature");
280276
}
281277
*done = true;
282278
state->state = FE_SCRAM_FINISHED;
283279
break;
284280

285281
default:
286282
/* shouldn't happen */
287-
appendPQExpBufferStr(&conn->errorMessage,
288-
libpq_gettext("invalid SCRAM exchange state\n"));
283+
libpq_append_conn_error(conn, "invalid SCRAM exchange state");
289284
goto error;
290285
}
291286
return;
@@ -311,18 +306,18 @@ read_attr_value(char **input, char attr, PQExpBuffer errorMessage)
311306

312307
if (*begin != attr)
313308
{
314-
appendPQExpBuffer(errorMessage,
315-
libpq_gettext("malformed SCRAM message (attribute \"%c\" expected)\n"),
316-
attr);
309+
libpq_append_error(errorMessage,
310+
"malformed SCRAM message (attribute \"%c\" expected)",
311+
attr);
317312
return NULL;
318313
}
319314
begin++;
320315

321316
if (*begin != '=')
322317
{
323-
appendPQExpBuffer(errorMessage,
324-
libpq_gettext("malformed SCRAM message (expected character \"=\" for attribute \"%c\")\n"),
325-
attr);
318+
libpq_append_error(errorMessage,
319+
"malformed SCRAM message (expected character \"=\" for attribute \"%c\")",
320+
attr);
326321
return NULL;
327322
}
328323
begin++;
@@ -361,8 +356,7 @@ build_client_first_message(fe_scram_state *state)
361356
*/
362357
if (!pg_strong_random(raw_nonce, SCRAM_RAW_NONCE_LEN))
363358
{
364-
appendPQExpBufferStr(&conn->errorMessage,
365-
libpq_gettext("could not generate nonce\n"));
359+
libpq_append_conn_error(conn, "could not generate nonce");
366360
return NULL;
367361
}
368362

@@ -371,16 +365,14 @@ build_client_first_message(fe_scram_state *state)
371365
state->client_nonce = malloc(encoded_len + 1);
372366
if (state->client_nonce == NULL)
373367
{
374-
appendPQExpBufferStr(&conn->errorMessage,
375-
libpq_gettext("out of memory\n"));
368+
libpq_append_conn_error(conn, "out of memory");
376369
return NULL;
377370
}
378371
encoded_len = pg_b64_encode(raw_nonce, SCRAM_RAW_NONCE_LEN,
379372
state->client_nonce, encoded_len);
380373
if (encoded_len < 0)
381374
{
382-
appendPQExpBufferStr(&conn->errorMessage,
383-
libpq_gettext("could not encode nonce\n"));
375+
libpq_append_conn_error(conn, "could not encode nonce");
384376
return NULL;
385377
}
386378
state->client_nonce[encoded_len] = '\0';
@@ -446,8 +438,7 @@ build_client_first_message(fe_scram_state *state)
446438

447439
oom_error:
448440
termPQExpBuffer(&buf);
449-
appendPQExpBufferStr(&conn->errorMessage,
450-
libpq_gettext("out of memory\n"));
441+
libpq_append_conn_error(conn, "out of memory");
451442
return NULL;
452443
}
453444

@@ -569,9 +560,7 @@ build_client_final_message(fe_scram_state *state)
569560
client_proof, &errstr))
570561
{
571562
termPQExpBuffer(&buf);
572-
appendPQExpBuffer(&conn->errorMessage,
573-
libpq_gettext("could not calculate client proof: %s\n"),
574-
errstr);
563+
libpq_append_conn_error(conn, "could not calculate client proof: %s", errstr);
575564
return NULL;
576565
}
577566

@@ -586,8 +575,7 @@ build_client_final_message(fe_scram_state *state)
586575
if (encoded_len < 0)
587576
{
588577
termPQExpBuffer(&buf);
589-
appendPQExpBufferStr(&conn->errorMessage,
590-
libpq_gettext("could not encode client proof\n"));
578+
libpq_append_conn_error(conn, "could not encode client proof");
591579
return NULL;
592580
}
593581
buf.len += encoded_len;
@@ -602,8 +590,7 @@ build_client_final_message(fe_scram_state *state)
602590

603591
oom_error:
604592
termPQExpBuffer(&buf);
605-
appendPQExpBufferStr(&conn->errorMessage,
606-
libpq_gettext("out of memory\n"));
593+
libpq_append_conn_error(conn, "out of memory");
607594
return NULL;
608595
}
609596

@@ -623,8 +610,7 @@ read_server_first_message(fe_scram_state *state, char *input)
623610
state->server_first_message = strdup(input);
624611
if (state->server_first_message == NULL)
625612
{
626-
appendPQExpBufferStr(&conn->errorMessage,
627-
libpq_gettext("out of memory\n"));
613+
libpq_append_conn_error(conn, "out of memory");
628614
return false;
629615
}
630616

@@ -641,16 +627,14 @@ read_server_first_message(fe_scram_state *state, char *input)
641627
if (strlen(nonce) < strlen(state->client_nonce) ||
642628
memcmp(nonce, state->client_nonce, strlen(state->client_nonce)) != 0)
643629
{
644-
appendPQExpBufferStr(&conn->errorMessage,
645-
libpq_gettext("invalid SCRAM response (nonce mismatch)\n"));
630+
libpq_append_conn_error(conn, "invalid SCRAM response (nonce mismatch)");
646631
return false;
647632
}
648633

649634
state->nonce = strdup(nonce);
650635
if (state->nonce == NULL)
651636
{
652-
appendPQExpBufferStr(&conn->errorMessage,
653-
libpq_gettext("out of memory\n"));
637+
libpq_append_conn_error(conn, "out of memory");
654638
return false;
655639
}
656640

@@ -664,8 +648,7 @@ read_server_first_message(fe_scram_state *state, char *input)
664648
state->salt = malloc(decoded_salt_len);
665649
if (state->salt == NULL)
666650
{
667-
appendPQExpBufferStr(&conn->errorMessage,
668-
libpq_gettext("out of memory\n"));
651+
libpq_append_conn_error(conn, "out of memory");
669652
return false;
670653
}
671654
state->saltlen = pg_b64_decode(encoded_salt,
@@ -674,8 +657,7 @@ read_server_first_message(fe_scram_state *state, char *input)
674657
decoded_salt_len);
675658
if (state->saltlen < 0)
676659
{
677-
appendPQExpBufferStr(&conn->errorMessage,
678-
libpq_gettext("malformed SCRAM message (invalid salt)\n"));
660+
libpq_append_conn_error(conn, "malformed SCRAM message (invalid salt)");
679661
return false;
680662
}
681663

@@ -688,14 +670,12 @@ read_server_first_message(fe_scram_state *state, char *input)
688670
state->iterations = strtol(iterations_str, &endptr, 10);
689671
if (*endptr != '\0' || state->iterations < 1)
690672
{
691-
appendPQExpBufferStr(&conn->errorMessage,
692-
libpq_gettext("malformed SCRAM message (invalid iteration count)\n"));
673+
libpq_append_conn_error(conn, "malformed SCRAM message (invalid iteration count)");
693674
return false;
694675
}
695676

696677
if (*input != '\0')
697-
appendPQExpBufferStr(&conn->errorMessage,
698-
libpq_gettext("malformed SCRAM message (garbage at end of server-first-message)\n"));
678+
libpq_append_conn_error(conn, "malformed SCRAM message (garbage at end of server-first-message)");
699679

700680
return true;
701681
}
@@ -714,8 +694,7 @@ read_server_final_message(fe_scram_state *state, char *input)
714694
state->server_final_message = strdup(input);
715695
if (!state->server_final_message)
716696
{
717-
appendPQExpBufferStr(&conn->errorMessage,
718-
libpq_gettext("out of memory\n"));
697+
libpq_append_conn_error(conn, "out of memory");
719698
return false;
720699
}
721700

@@ -730,9 +709,8 @@ read_server_final_message(fe_scram_state *state, char *input)
730709
/* read_attr_value() has appended an error message */
731710
return false;
732711
}
733-
appendPQExpBuffer(&conn->errorMessage,
734-
libpq_gettext("error received from server in SCRAM exchange: %s\n"),
735-
errmsg);
712+
libpq_append_conn_error(conn, "error received from server in SCRAM exchange: %s",
713+
errmsg);
736714
return false;
737715
}
738716

@@ -746,15 +724,13 @@ read_server_final_message(fe_scram_state *state, char *input)
746724
}
747725

748726
if (*input != '\0')
749-
appendPQExpBufferStr(&conn->errorMessage,
750-
libpq_gettext("malformed SCRAM message (garbage at end of server-final-message)\n"));
727+
libpq_append_conn_error(conn, "malformed SCRAM message (garbage at end of server-final-message)");
751728

752729
server_signature_len = pg_b64_dec_len(strlen(encoded_server_signature));
753730
decoded_server_signature = malloc(server_signature_len);
754731
if (!decoded_server_signature)
755732
{
756-
appendPQExpBufferStr(&conn->errorMessage,
757-
libpq_gettext("out of memory\n"));
733+
libpq_append_conn_error(conn, "out of memory");
758734
return false;
759735
}
760736

@@ -765,8 +741,7 @@ read_server_final_message(fe_scram_state *state, char *input)
765741
if (server_signature_len != SCRAM_KEY_LEN)
766742
{
767743
free(decoded_server_signature);
768-
appendPQExpBufferStr(&conn->errorMessage,
769-
libpq_gettext("malformed SCRAM message (invalid server signature)\n"));
744+
libpq_append_conn_error(conn, "malformed SCRAM message (invalid server signature)");
770745
return false;
771746
}
772747
memcpy(state->ServerSignature, decoded_server_signature, SCRAM_KEY_LEN);

0 commit comments

Comments
 (0)