Skip to content

Commit b7103bb

Browse files
committed
Avoid direct C access to possibly-null pg_subscription_rel.srsublsn.
This coding technique is unsafe, since we'd be accessing off the end of the tuple if the field is null. SIGSEGV is pretty improbable, but perhaps not impossible. Also, returning garbage for the LSN doesn't seem like a great idea, even if callers aren't looking at it today. Also update docs to point out explicitly that pg_subscription.subslotname and pg_subscription_rel.srsublsn can be null. Perhaps we should mark these two fields BKI_FORCE_NULL, so that they'd be correctly labeled in databases that are initdb'd in the future. But we can't force that for existing databases, and on balance it's not too clear that having a mix of different catalog contents in the field would be wise. Apply to v10 (where this code came in) through v12. Already fixed in v13 and HEAD. Discussion: https://postgr.es/m/732838.1595278439@sss.pgh.pa.us
1 parent 798b4fa commit b7103bb

File tree

3 files changed

+33
-7
lines changed

3 files changed

+33
-7
lines changed

doc/src/sgml/catalogs.sgml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6786,8 +6786,9 @@ SCRAM-SHA-256$<replaceable>&lt;iteration count&gt;</replaceable>:<replaceable>&l
67866786
<entry><structfield>subslotname</structfield></entry>
67876787
<entry><type>name</type></entry>
67886788
<entry></entry>
6789-
<entry>Name of the replication slot in the upstream database. Also used
6790-
for local replication origin name.</entry>
6789+
<entry>Name of the replication slot in the upstream database (also used
6790+
for the local replication origin name);
6791+
null represents <literal>NONE</literal></entry>
67916792
</row>
67926793

67936794
<row>
@@ -6869,7 +6870,9 @@ SCRAM-SHA-256$<replaceable>&lt;iteration count&gt;</replaceable>:<replaceable>&l
68696870
<entry><type>pg_lsn</type></entry>
68706871
<entry></entry>
68716872
<entry>
6872-
End LSN for <literal>s</literal> and <literal>r</literal> states.
6873+
Remote LSN of the state change used for synchronization coordination
6874+
when in <literal>s</literal> or <literal>r</literal> states,
6875+
otherwise null
68736876
</entry>
68746877
</row>
68756878
</tbody>

src/backend/catalog/pg_subscription.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -457,13 +457,20 @@ GetSubscriptionRelations(Oid subid)
457457
{
458458
Form_pg_subscription_rel subrel;
459459
SubscriptionRelState *relstate;
460+
Datum d;
461+
bool isnull;
460462

461463
subrel = (Form_pg_subscription_rel) GETSTRUCT(tup);
462464

463465
relstate = (SubscriptionRelState *) palloc(sizeof(SubscriptionRelState));
464466
relstate->relid = subrel->srrelid;
465467
relstate->state = subrel->srsubstate;
466-
relstate->lsn = subrel->srsublsn;
468+
d = SysCacheGetAttr(SUBSCRIPTIONRELMAP, tup,
469+
Anum_pg_subscription_rel_srsublsn, &isnull);
470+
if (isnull)
471+
relstate->lsn = InvalidXLogRecPtr;
472+
else
473+
relstate->lsn = DatumGetLSN(d);
467474

468475
res = lappend(res, relstate);
469476
}
@@ -509,13 +516,20 @@ GetSubscriptionNotReadyRelations(Oid subid)
509516
{
510517
Form_pg_subscription_rel subrel;
511518
SubscriptionRelState *relstate;
519+
Datum d;
520+
bool isnull;
512521

513522
subrel = (Form_pg_subscription_rel) GETSTRUCT(tup);
514523

515524
relstate = (SubscriptionRelState *) palloc(sizeof(SubscriptionRelState));
516525
relstate->relid = subrel->srrelid;
517526
relstate->state = subrel->srsubstate;
518-
relstate->lsn = subrel->srsublsn;
527+
d = SysCacheGetAttr(SUBSCRIPTIONRELMAP, tup,
528+
Anum_pg_subscription_rel_srsublsn, &isnull);
529+
if (isnull)
530+
relstate->lsn = InvalidXLogRecPtr;
531+
else
532+
relstate->lsn = DatumGetLSN(d);
519533

520534
res = lappend(res, relstate);
521535
}

src/include/catalog/pg_subscription_rel.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,17 @@ CATALOG(pg_subscription_rel,6102,SubscriptionRelRelationId)
3434
Oid srsubid; /* Oid of subscription */
3535
Oid srrelid; /* Oid of relation */
3636
char srsubstate; /* state of the relation in subscription */
37-
XLogRecPtr srsublsn; /* remote lsn of the state change used for
38-
* synchronization coordination */
37+
38+
/*
39+
* Although srsublsn is a fixed-width type, it is allowed to be NULL, so
40+
* we prevent direct C code access to it just as for a varlena field.
41+
*/
42+
#ifdef CATALOG_VARLEN /* variable-length fields start here */
43+
44+
XLogRecPtr srsublsn; /* remote LSN of the state change used for
45+
* synchronization coordination, or NULL if
46+
* not valid */
47+
#endif
3948
} FormData_pg_subscription_rel;
4049

4150
typedef FormData_pg_subscription_rel *Form_pg_subscription_rel;

0 commit comments

Comments
 (0)