Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit ccd415c

Browse files
committed
Fix unportable assumptions about alignment of local char[n] variables.
1 parent 571dbe4 commit ccd415c

File tree

4 files changed

+82
-53
lines changed

4 files changed

+82
-53
lines changed

src/backend/access/heap/heapam.c

+58-29
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/access/heap/heapam.c,v 1.112 2001/03/22 06:16:07 momjian Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/access/heap/heapam.c,v 1.113 2001/03/25 23:23:58 tgl Exp $
1212
*
1313
*
1414
* INTERFACE ROUTINES
@@ -2126,10 +2126,19 @@ static XLogRecPtr
21262126
log_heap_update(Relation reln, Buffer oldbuf, ItemPointerData from,
21272127
Buffer newbuf, HeapTuple newtup, bool move)
21282128
{
2129-
char tbuf[MAXALIGN(sizeof(xl_heap_header)) + 2 * sizeof(TransactionId)];
2130-
xl_heap_update xlrec;
2131-
xl_heap_header *xlhdr = (xl_heap_header *) tbuf;
2129+
/*
2130+
* Note: xlhdr is declared to have adequate size and correct alignment
2131+
* for an xl_heap_header. However the two tids, if present at all,
2132+
* will be packed in with no wasted space after the xl_heap_header;
2133+
* they aren't necessarily aligned as implied by this struct declaration.
2134+
*/
2135+
struct {
2136+
xl_heap_header hdr;
2137+
TransactionId tid1;
2138+
TransactionId tid2;
2139+
} xlhdr;
21322140
int hsize = SizeOfHeapHeader;
2141+
xl_heap_update xlrec;
21332142
XLogRecPtr recptr;
21342143
XLogRecData rdata[4];
21352144
Page page = BufferGetPage(newbuf);
@@ -2148,10 +2157,10 @@ log_heap_update(Relation reln, Buffer oldbuf, ItemPointerData from,
21482157
rdata[1].len = 0;
21492158
rdata[1].next = &(rdata[2]);
21502159

2151-
xlhdr->t_oid = newtup->t_data->t_oid;
2152-
xlhdr->t_natts = newtup->t_data->t_natts;
2153-
xlhdr->t_hoff = newtup->t_data->t_hoff;
2154-
xlhdr->mask = newtup->t_data->t_infomask;
2160+
xlhdr.hdr.t_oid = newtup->t_data->t_oid;
2161+
xlhdr.hdr.t_natts = newtup->t_data->t_natts;
2162+
xlhdr.hdr.t_hoff = newtup->t_data->t_hoff;
2163+
xlhdr.hdr.mask = newtup->t_data->t_infomask;
21552164
if (move) /* remember xmin & xmax */
21562165
{
21572166
TransactionId xmax;
@@ -2161,13 +2170,13 @@ log_heap_update(Relation reln, Buffer oldbuf, ItemPointerData from,
21612170
xmax = InvalidTransactionId;
21622171
else
21632172
xmax = newtup->t_data->t_xmax;
2164-
memcpy(tbuf + hsize, &xmax, sizeof(TransactionId));
2165-
memcpy(tbuf + hsize + sizeof(TransactionId),
2173+
memcpy((char *) &xlhdr + hsize, &xmax, sizeof(TransactionId));
2174+
memcpy((char *) &xlhdr + hsize + sizeof(TransactionId),
21662175
&(newtup->t_data->t_xmin), sizeof(TransactionId));
2167-
hsize += (2 * sizeof(TransactionId));
2176+
hsize += 2 * sizeof(TransactionId);
21682177
}
21692178
rdata[2].buffer = newbuf;
2170-
rdata[2].data = (char *) xlhdr;
2179+
rdata[2].data = (char *) &xlhdr;
21712180
rdata[2].len = hsize;
21722181
rdata[2].next = &(rdata[3]);
21732182

@@ -2228,13 +2237,16 @@ heap_xlog_clean(bool redo, XLogRecPtr lsn, XLogRecord *record)
22282237

22292238
if (record->xl_len > SizeOfHeapClean)
22302239
{
2231-
char unbuf[BLCKSZ];
2232-
OffsetNumber *unused = (OffsetNumber *) unbuf;
2240+
OffsetNumber unbuf[BLCKSZ/sizeof(OffsetNumber)];
2241+
OffsetNumber *unused = unbuf;
22332242
char *unend;
22342243
ItemId lp;
22352244

2236-
memcpy(unbuf, (char *) xlrec + SizeOfHeapClean, record->xl_len - SizeOfHeapClean);
2237-
unend = unbuf + (record->xl_len - SizeOfHeapClean);
2245+
Assert((record->xl_len - SizeOfHeapClean) <= BLCKSZ);
2246+
memcpy((char *) unbuf,
2247+
(char *) xlrec + SizeOfHeapClean,
2248+
record->xl_len - SizeOfHeapClean);
2249+
unend = (char *) unbuf + (record->xl_len - SizeOfHeapClean);
22382250

22392251
while ((char *) unused < unend)
22402252
{
@@ -2318,7 +2330,6 @@ heap_xlog_insert(bool redo, XLogRecPtr lsn, XLogRecord *record)
23182330
Buffer buffer;
23192331
Page page;
23202332
OffsetNumber offnum;
2321-
HeapTupleHeader htup;
23222333

23232334
if (redo && (record->xl_info & XLR_BKP_BLOCK_1))
23242335
return;
@@ -2338,7 +2349,11 @@ heap_xlog_insert(bool redo, XLogRecPtr lsn, XLogRecord *record)
23382349

23392350
if (redo)
23402351
{
2341-
char tbuf[MaxTupleSize];
2352+
struct {
2353+
HeapTupleHeaderData hdr;
2354+
char data[MaxTupleSize];
2355+
} tbuf;
2356+
HeapTupleHeader htup;
23422357
xl_heap_header xlhdr;
23432358
uint32 newlen;
23442359

@@ -2359,11 +2374,15 @@ heap_xlog_insert(bool redo, XLogRecPtr lsn, XLogRecord *record)
23592374
elog(STOP, "heap_insert_redo: invalid max offset number");
23602375

23612376
newlen = record->xl_len - SizeOfHeapInsert - SizeOfHeapHeader;
2362-
memcpy((char *) &xlhdr, (char *) xlrec + SizeOfHeapInsert, SizeOfHeapHeader);
2363-
memcpy(tbuf + offsetof(HeapTupleHeaderData, t_bits),
2364-
(char *) xlrec + SizeOfHeapInsert + SizeOfHeapHeader, newlen);
2377+
Assert(newlen <= MaxTupleSize);
2378+
memcpy((char *) &xlhdr,
2379+
(char *) xlrec + SizeOfHeapInsert,
2380+
SizeOfHeapHeader);
2381+
memcpy((char *) &tbuf + offsetof(HeapTupleHeaderData, t_bits),
2382+
(char *) xlrec + SizeOfHeapInsert + SizeOfHeapHeader,
2383+
newlen);
23652384
newlen += offsetof(HeapTupleHeaderData, t_bits);
2366-
htup = (HeapTupleHeader) tbuf;
2385+
htup = &tbuf.hdr;
23672386
htup->t_oid = xlhdr.t_oid;
23682387
htup->t_natts = xlhdr.t_natts;
23692388
htup->t_hoff = xlhdr.t_hoff;
@@ -2496,7 +2515,10 @@ newsame:;
24962515

24972516
if (redo)
24982517
{
2499-
char tbuf[MaxTupleSize];
2518+
struct {
2519+
HeapTupleHeaderData hdr;
2520+
char data[MaxTupleSize];
2521+
} tbuf;
25002522
xl_heap_header xlhdr;
25012523
int hsize;
25022524
uint32 newlen;
@@ -2522,20 +2544,27 @@ newsame:;
25222544
hsize += (2 * sizeof(TransactionId));
25232545

25242546
newlen = record->xl_len - hsize;
2525-
memcpy((char *) &xlhdr, (char *) xlrec + SizeOfHeapUpdate, SizeOfHeapHeader);
2526-
memcpy(tbuf + offsetof(HeapTupleHeaderData, t_bits),
2527-
(char *) xlrec + hsize, newlen);
2547+
Assert(newlen <= MaxTupleSize);
2548+
memcpy((char *) &xlhdr,
2549+
(char *) xlrec + SizeOfHeapUpdate,
2550+
SizeOfHeapHeader);
2551+
memcpy((char *) &tbuf + offsetof(HeapTupleHeaderData, t_bits),
2552+
(char *) xlrec + hsize,
2553+
newlen);
25282554
newlen += offsetof(HeapTupleHeaderData, t_bits);
2529-
htup = (HeapTupleHeader) tbuf;
2555+
htup = &tbuf.hdr;
25302556
htup->t_oid = xlhdr.t_oid;
25312557
htup->t_natts = xlhdr.t_natts;
25322558
htup->t_hoff = xlhdr.t_hoff;
25332559
if (move)
25342560
{
25352561
hsize = SizeOfHeapUpdate + SizeOfHeapHeader;
2536-
memcpy(&(htup->t_xmax), (char *) xlrec + hsize, sizeof(TransactionId));
2562+
memcpy(&(htup->t_xmax),
2563+
(char *) xlrec + hsize,
2564+
sizeof(TransactionId));
25372565
memcpy(&(htup->t_xmin),
2538-
(char *) xlrec + hsize + sizeof(TransactionId), sizeof(TransactionId));
2566+
(char *) xlrec + hsize + sizeof(TransactionId),
2567+
sizeof(TransactionId));
25392568
TransactionIdStore(record->xl_xid, (TransactionId *) &(htup->t_cmin));
25402569
htup->t_infomask = xlhdr.mask;
25412570
htup->t_infomask &= ~(HEAP_XMIN_COMMITTED |

src/backend/commands/vacuum.c

+9-9
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/commands/vacuum.c,v 1.188 2001/03/22 03:59:24 momjian Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/commands/vacuum.c,v 1.189 2001/03/25 23:23:58 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -303,10 +303,9 @@ getrels(NameData *VacRelP)
303303
found = true;
304304

305305
d = heap_getattr(tuple, Anum_pg_class_relname, tupdesc, &n);
306-
rname = (char *) d;
306+
rname = (char *) DatumGetPointer(d);
307307

308308
d = heap_getattr(tuple, Anum_pg_class_relkind, tupdesc, &n);
309-
310309
rkind = DatumGetChar(d);
311310

312311
if (rkind != RELKIND_RELATION)
@@ -997,8 +996,8 @@ repair_frag(VRelStats *vacrelstats, Relation onerel,
997996
blkno;
998997
Page page,
999998
ToPage = NULL;
1000-
OffsetNumber offnum = 0,
1001-
maxoff = 0,
999+
OffsetNumber offnum,
1000+
maxoff,
10021001
newoff,
10031002
max_offset;
10041003
ItemId itemid,
@@ -1913,14 +1912,15 @@ failed to add item with len = %lu to page %u (free space %lu, nusd %u, noff %u)"
19131912
if (vacpage->blkno == (BlockNumber) (blkno - 1) &&
19141913
vacpage->offsets_free > 0)
19151914
{
1916-
char unbuf[BLCKSZ];
1917-
OffsetNumber *unused = (OffsetNumber *) unbuf;
1915+
OffsetNumber unbuf[BLCKSZ/sizeof(OffsetNumber)];
1916+
OffsetNumber *unused = unbuf;
19181917
int uncnt;
19191918

19201919
buf = ReadBuffer(onerel, vacpage->blkno);
19211920
LockBuffer(buf, BUFFER_LOCK_EXCLUSIVE);
19221921
page = BufferGetPage(buf);
19231922
num_tuples = 0;
1923+
maxoff = PageGetMaxOffsetNumber(page);
19241924
for (offnum = FirstOffsetNumber;
19251925
offnum <= maxoff;
19261926
offnum = OffsetNumberNext(offnum))
@@ -2061,8 +2061,8 @@ vacuum_heap(VRelStats *vacrelstats, Relation onerel, VacPageList vacuum_pages)
20612061
static void
20622062
vacuum_page(Relation onerel, Buffer buffer, VacPage vacpage)
20632063
{
2064-
char unbuf[BLCKSZ];
2065-
OffsetNumber *unused = (OffsetNumber *) unbuf;
2064+
OffsetNumber unbuf[BLCKSZ/sizeof(OffsetNumber)];
2065+
OffsetNumber *unused = unbuf;
20662066
int uncnt;
20672067
Page page = BufferGetPage(buffer);
20682068
ItemId itemid;

src/backend/storage/large_object/inv_api.c

+10-7
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*
1010
*
1111
* IDENTIFICATION
12-
* $Header: /cvsroot/pgsql/src/backend/storage/large_object/inv_api.c,v 1.86 2001/03/22 03:59:45 momjian Exp $
12+
* $Header: /cvsroot/pgsql/src/backend/storage/large_object/inv_api.c,v 1.87 2001/03/25 23:23:59 tgl Exp $
1313
*
1414
*-------------------------------------------------------------------------
1515
*/
@@ -416,8 +416,11 @@ inv_write(LargeObjectDesc *obj_desc, char *buf, int nbytes)
416416
bool neednextpage;
417417
bytea *datafield;
418418
bool pfreeit;
419-
char workbuf[LOBLKSIZE + VARHDRSZ];
420-
char *workb = VARATT_DATA(workbuf);
419+
struct {
420+
struct varlena hdr;
421+
char data[LOBLKSIZE];
422+
} workbuf;
423+
char *workb = VARATT_DATA(&workbuf.hdr);
421424
HeapTuple newtup;
422425
Datum values[Natts_pg_largeobject];
423426
char nulls[Natts_pg_largeobject];
@@ -526,15 +529,15 @@ inv_write(LargeObjectDesc *obj_desc, char *buf, int nbytes)
526529
off += n;
527530
/* compute valid length of new page */
528531
len = (len >= off) ? len : off;
529-
VARATT_SIZEP(workbuf) = len + VARHDRSZ;
532+
VARATT_SIZEP(&workbuf.hdr) = len + VARHDRSZ;
530533

531534
/*
532535
* Form and insert updated tuple
533536
*/
534537
memset(values, 0, sizeof(values));
535538
memset(nulls, ' ', sizeof(nulls));
536539
memset(replace, ' ', sizeof(replace));
537-
values[Anum_pg_largeobject_data - 1] = PointerGetDatum(workbuf);
540+
values[Anum_pg_largeobject_data - 1] = PointerGetDatum(&workbuf);
538541
replace[Anum_pg_largeobject_data - 1] = 'r';
539542
newtup = heap_modifytuple(&oldtuple, obj_desc->heap_r,
540543
values, nulls, replace);
@@ -575,7 +578,7 @@ inv_write(LargeObjectDesc *obj_desc, char *buf, int nbytes)
575578
obj_desc->offset += n;
576579
/* compute valid length of new page */
577580
len = off + n;
578-
VARATT_SIZEP(workbuf) = len + VARHDRSZ;
581+
VARATT_SIZEP(&workbuf.hdr) = len + VARHDRSZ;
579582

580583
/*
581584
* Form and insert updated tuple
@@ -584,7 +587,7 @@ inv_write(LargeObjectDesc *obj_desc, char *buf, int nbytes)
584587
memset(nulls, ' ', sizeof(nulls));
585588
values[Anum_pg_largeobject_loid - 1] = ObjectIdGetDatum(obj_desc->id);
586589
values[Anum_pg_largeobject_pageno - 1] = Int32GetDatum(pageno);
587-
values[Anum_pg_largeobject_data - 1] = PointerGetDatum(workbuf);
590+
values[Anum_pg_largeobject_data - 1] = PointerGetDatum(&workbuf);
588591
newtup = heap_formtuple(obj_desc->heap_r->rd_att, values, nulls);
589592
heap_insert(obj_desc->heap_r, newtup);
590593
if (write_indices)

src/backend/utils/mb/wstrncmp.c

+5-8
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,12 @@ register const pg_wchar *s1,
4343
*s2;
4444
register size_t n;
4545
{
46-
4746
if (n == 0)
4847
return 0;
4948
do
5049
{
5150
if (*s1 != *s2++)
52-
return (*(const pg_wchar *) s1 -
53-
*(const pg_wchar *) (s2 - 1));
51+
return (*s1 - *(s2 - 1));
5452
if (*s1++ == 0)
5553
break;
5654
} while (--n != 0);
@@ -63,14 +61,12 @@ register const char *s1;
6361
register const pg_wchar *s2;
6462
register size_t n;
6563
{
66-
6764
if (n == 0)
6865
return 0;
6966
do
7067
{
71-
if ((pg_wchar) * s1 != *s2++)
72-
return (*(const pg_wchar *) s1 -
73-
*(const pg_wchar *) (s2 - 1));
68+
if ((pg_wchar) ((unsigned char) *s1) != *s2++)
69+
return ((pg_wchar) ((unsigned char) *s1) - *(s2 - 1));
7470
if (*s1++ == 0)
7571
break;
7672
} while (--n != 0);
@@ -83,6 +79,7 @@ const pg_wchar *str;
8379
{
8480
register const pg_wchar *s;
8581

86-
for (s = str; *s; ++s);
82+
for (s = str; *s; ++s)
83+
;
8784
return (s - str);
8885
}

0 commit comments

Comments
 (0)