Skip to content

Commit fa17165

Browse files
committed
Measure string lengths only once
Bernd Helmle complained that CreateReplicationSlot() was assigning the same value to the same variable twice, so we could remove one of them. Code inspection reveals that we can actually remove both assignments: according to the author the assignment was there for beauty of the strlen line only, and another possible fix to that is to put the strlen in its own line, so do that. To be consistent within the file, refactor all duplicated strlen() calls, which is what we do elsewhere in the backend anyway. In basebackup.c, snprintf already returns the right length; no need for strlen afterwards. Backpatch to 9.4, where replication slots were introduced, to keep code identical. Some of this is older, but the patch doesn't apply cleanly and it's only of cosmetic value anyway. Discussion: http://www.postgresql.org/message-id/BE2FD71DEA35A2287EA5F018@eje.credativ.lan
1 parent 5eca6cf commit fa17165

File tree

2 files changed

+70
-45
lines changed

2 files changed

+70
-45
lines changed

src/backend/replication/basebackup.c

+19-11
Original file line numberDiff line numberDiff line change
@@ -748,10 +748,15 @@ SendBackupHeader(List *tablespaces)
748748
}
749749
else
750750
{
751-
pq_sendint(&buf, strlen(ti->oid), 4); /* length */
752-
pq_sendbytes(&buf, ti->oid, strlen(ti->oid));
753-
pq_sendint(&buf, strlen(ti->path), 4); /* length */
754-
pq_sendbytes(&buf, ti->path, strlen(ti->path));
751+
Size len;
752+
753+
len = strlen(ti->oid);
754+
pq_sendint(&buf, len, 4);
755+
pq_sendbytes(&buf, ti->oid, len);
756+
757+
len = strlen(ti->path);
758+
pq_sendint(&buf, len, 4);
759+
pq_sendbytes(&buf, ti->path, len);
755760
}
756761
if (ti->size >= 0)
757762
send_int8_string(&buf, ti->size / 1024);
@@ -774,6 +779,7 @@ SendXlogRecPtrResult(XLogRecPtr ptr, TimeLineID tli)
774779
{
775780
StringInfoData buf;
776781
char str[MAXFNAMELEN];
782+
Size len;
777783

778784
pq_beginmessage(&buf, 'T'); /* RowDescription */
779785
pq_sendint(&buf, 2, 2); /* 2 fields */
@@ -792,7 +798,7 @@ SendXlogRecPtrResult(XLogRecPtr ptr, TimeLineID tli)
792798
pq_sendint(&buf, 0, 2); /* attnum */
793799

794800
/*
795-
* int8 may seem like a surprising data type for this, but in thory int4
801+
* int8 may seem like a surprising data type for this, but in theory int4
796802
* would not be wide enough for this, as TimeLineID is unsigned.
797803
*/
798804
pq_sendint(&buf, INT8OID, 4); /* type oid */
@@ -805,13 +811,15 @@ SendXlogRecPtrResult(XLogRecPtr ptr, TimeLineID tli)
805811
pq_beginmessage(&buf, 'D');
806812
pq_sendint(&buf, 2, 2); /* number of columns */
807813

808-
snprintf(str, sizeof(str), "%X/%X", (uint32) (ptr >> 32), (uint32) ptr);
809-
pq_sendint(&buf, strlen(str), 4); /* length */
810-
pq_sendbytes(&buf, str, strlen(str));
814+
len = snprintf(str, sizeof(str),
815+
"%X/%X", (uint32) (ptr >> 32), (uint32) ptr);
816+
pq_sendint(&buf, len, 4);
817+
pq_sendbytes(&buf, str, len);
818+
819+
len = snprintf(str, sizeof(str), "%u", tli);
820+
pq_sendint(&buf, len, 4);
821+
pq_sendbytes(&buf, str, len);
811822

812-
snprintf(str, sizeof(str), "%u", tli);
813-
pq_sendint(&buf, strlen(str), 4); /* length */
814-
pq_sendbytes(&buf, str, strlen(str));
815823
pq_endmessage(&buf);
816824

817825
/* Send a CommandComplete message */

src/backend/replication/walsender.c

+51-34
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ IdentifySystem(void)
298298
char xpos[MAXFNAMELEN];
299299
XLogRecPtr logptr;
300300
char *dbname = NULL;
301+
Size len;
301302

302303
/*
303304
* Reply with a result set with one row, four columns. First col is system
@@ -379,21 +380,32 @@ IdentifySystem(void)
379380
/* Send a DataRow message */
380381
pq_beginmessage(&buf, 'D');
381382
pq_sendint(&buf, 4, 2); /* # of columns */
382-
pq_sendint(&buf, strlen(sysid), 4); /* col1 len */
383-
pq_sendbytes(&buf, (char *) &sysid, strlen(sysid));
384-
pq_sendint(&buf, strlen(tli), 4); /* col2 len */
385-
pq_sendbytes(&buf, (char *) tli, strlen(tli));
386-
pq_sendint(&buf, strlen(xpos), 4); /* col3 len */
387-
pq_sendbytes(&buf, (char *) xpos, strlen(xpos));
388-
/* send NULL if not connected to a database */
383+
384+
/* column 1: system identifier */
385+
len = strlen(sysid);
386+
pq_sendint(&buf, len, 4);
387+
pq_sendbytes(&buf, (char *) &sysid, len);
388+
389+
/* column 2: timeline */
390+
len = strlen(tli);
391+
pq_sendint(&buf, len, 4);
392+
pq_sendbytes(&buf, (char *) tli, len);
393+
394+
/* column 3: xlog position */
395+
len = strlen(xpos);
396+
pq_sendint(&buf, len, 4);
397+
pq_sendbytes(&buf, (char *) xpos, len);
398+
399+
/* column 4: database name, or NULL if none */
389400
if (dbname)
390401
{
391-
pq_sendint(&buf, strlen(dbname), 4); /* col4 len */
392-
pq_sendbytes(&buf, (char *) dbname, strlen(dbname));
402+
len = strlen(dbname);
403+
pq_sendint(&buf, len, 4);
404+
pq_sendbytes(&buf, (char *) dbname, len);
393405
}
394406
else
395407
{
396-
pq_sendint(&buf, -1, 4); /* col4 len, NULL */
408+
pq_sendint(&buf, -1, 4);
397409
}
398410

399411
pq_endmessage(&buf);
@@ -412,6 +424,7 @@ SendTimeLineHistory(TimeLineHistoryCmd *cmd)
412424
int fd;
413425
off_t histfilelen;
414426
off_t bytesleft;
427+
Size len;
415428

416429
/*
417430
* Reply with a result set with one row, and two columns. The first col is
@@ -447,8 +460,9 @@ SendTimeLineHistory(TimeLineHistoryCmd *cmd)
447460
/* Send a DataRow message */
448461
pq_beginmessage(&buf, 'D');
449462
pq_sendint(&buf, 2, 2); /* # of columns */
450-
pq_sendint(&buf, strlen(histfname), 4); /* col1 len */
451-
pq_sendbytes(&buf, histfname, strlen(histfname));
463+
len = strlen(histfname);
464+
pq_sendint(&buf, len, 4); /* col1 len */
465+
pq_sendbytes(&buf, histfname, len);
452466

453467
fd = OpenTransientFile(path, O_RDONLY | PG_BINARY, 0666);
454468
if (fd < 0)
@@ -674,6 +688,7 @@ StartReplication(StartReplicationCmd *cmd)
674688
{
675689
char tli_str[11];
676690
char startpos_str[8 + 1 + 8 + 1];
691+
Size len;
677692

678693
snprintf(tli_str, sizeof(tli_str), "%u", sendTimeLineNextTLI);
679694
snprintf(startpos_str, sizeof(startpos_str), "%X/%X",
@@ -710,11 +725,13 @@ StartReplication(StartReplicationCmd *cmd)
710725
pq_beginmessage(&buf, 'D');
711726
pq_sendint(&buf, 2, 2); /* number of columns */
712727

713-
pq_sendint(&buf, strlen(tli_str), 4); /* length */
714-
pq_sendbytes(&buf, tli_str, strlen(tli_str));
728+
len = strlen(tli_str);
729+
pq_sendint(&buf, len, 4); /* length */
730+
pq_sendbytes(&buf, tli_str, len);
715731

716-
pq_sendint(&buf, strlen(startpos_str), 4); /* length */
717-
pq_sendbytes(&buf, startpos_str, strlen(startpos_str));
732+
len = strlen(startpos_str);
733+
pq_sendint(&buf, len, 4); /* length */
734+
pq_sendbytes(&buf, startpos_str, len);
718735

719736
pq_endmessage(&buf);
720737
}
@@ -762,10 +779,10 @@ logical_read_xlog_page(XLogReaderState *state, XLogRecPtr targetPagePtr, int req
762779
static void
763780
CreateReplicationSlot(CreateReplicationSlotCmd *cmd)
764781
{
765-
const char *slot_name;
766782
const char *snapshot_name = NULL;
767783
char xpos[MAXFNAMELEN];
768784
StringInfoData buf;
785+
Size len;
769786

770787
Assert(!MyReplicationSlot);
771788

@@ -790,14 +807,11 @@ CreateReplicationSlot(CreateReplicationSlotCmd *cmd)
790807

791808
initStringInfo(&output_message);
792809

793-
slot_name = NameStr(MyReplicationSlot->data.name);
794-
795810
if (cmd->kind == REPLICATION_KIND_LOGICAL)
796811
{
797812
LogicalDecodingContext *ctx;
798813

799-
ctx = CreateInitDecodingContext(
800-
cmd->plugin, NIL,
814+
ctx = CreateInitDecodingContext(cmd->plugin, NIL,
801815
logical_read_xlog_page,
802816
WalSndPrepareWrite, WalSndWriteData);
803817

@@ -825,7 +839,6 @@ CreateReplicationSlot(CreateReplicationSlotCmd *cmd)
825839
ReplicationSlotPersist();
826840
}
827841

828-
slot_name = NameStr(MyReplicationSlot->data.name);
829842
snprintf(xpos, sizeof(xpos), "%X/%X",
830843
(uint32) (MyReplicationSlot->data.confirmed_flush >> 32),
831844
(uint32) MyReplicationSlot->data.confirmed_flush);
@@ -876,30 +889,34 @@ CreateReplicationSlot(CreateReplicationSlotCmd *cmd)
876889
pq_sendint(&buf, 4, 2); /* # of columns */
877890

878891
/* slot_name */
879-
pq_sendint(&buf, strlen(slot_name), 4); /* col1 len */
880-
pq_sendbytes(&buf, slot_name, strlen(slot_name));
892+
len = strlen(NameStr(MyReplicationSlot->data.name));
893+
pq_sendint(&buf, len, 4); /* col1 len */
894+
pq_sendbytes(&buf, NameStr(MyReplicationSlot->data.name), len);
881895

882896
/* consistent wal location */
883-
pq_sendint(&buf, strlen(xpos), 4); /* col2 len */
884-
pq_sendbytes(&buf, xpos, strlen(xpos));
897+
len = strlen(xpos);
898+
pq_sendint(&buf, len, 4);
899+
pq_sendbytes(&buf, xpos, len);
885900

886-
/* snapshot name */
901+
/* snapshot name, or NULL if none */
887902
if (snapshot_name != NULL)
888903
{
889-
pq_sendint(&buf, strlen(snapshot_name), 4); /* col3 len */
890-
pq_sendbytes(&buf, snapshot_name, strlen(snapshot_name));
904+
len = strlen(snapshot_name);
905+
pq_sendint(&buf, len, 4);
906+
pq_sendbytes(&buf, snapshot_name, len);
891907
}
892908
else
893-
pq_sendint(&buf, -1, 4); /* col3 len, NULL */
909+
pq_sendint(&buf, -1, 4);
894910

895-
/* plugin */
911+
/* plugin, or NULL if none */
896912
if (cmd->plugin != NULL)
897913
{
898-
pq_sendint(&buf, strlen(cmd->plugin), 4); /* col4 len */
899-
pq_sendbytes(&buf, cmd->plugin, strlen(cmd->plugin));
914+
len = strlen(cmd->plugin);
915+
pq_sendint(&buf, len, 4);
916+
pq_sendbytes(&buf, cmd->plugin, len);
900917
}
901918
else
902-
pq_sendint(&buf, -1, 4); /* col4 len, NULL */
919+
pq_sendint(&buf, -1, 4);
903920

904921
pq_endmessage(&buf);
905922

0 commit comments

Comments
 (0)