Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit84ad68d

Browse files
committed
pageinspect: Fix unaligned struct access in GIN functions
The raw page data that is passed into the functions will not be alignedat 8-byte boundaries. Casting that to a struct and accessing int64fields will result in unaligned access. On most platforms, you get awaywith it, but it will result on a crash on pickier platforms such as ia64and sparc64.
1 parentf2e6a2c commit84ad68d

File tree

1 file changed

+24
-22
lines changed

1 file changed

+24
-22
lines changed

‎contrib/pageinspect/ginfuncs.c

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,31 @@ PG_FUNCTION_INFO_V1(gin_metapage_info);
2828
PG_FUNCTION_INFO_V1(gin_page_opaque_info);
2929
PG_FUNCTION_INFO_V1(gin_leafpage_items);
3030

31+
32+
staticPage
33+
get_page_from_raw(bytea*raw_page)
34+
{
35+
intraw_page_size;
36+
Pagepage;
37+
38+
raw_page_size=VARSIZE(raw_page)-VARHDRSZ;
39+
if (raw_page_size<BLCKSZ)
40+
ereport(ERROR,
41+
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
42+
errmsg("input page too small (%d bytes)",raw_page_size)));
43+
44+
/* make a copy so that the page is properly aligned for struct access */
45+
page=palloc(raw_page_size);
46+
memcpy(page,VARDATA(raw_page),raw_page_size);
47+
48+
returnpage;
49+
}
50+
51+
3152
Datum
3253
gin_metapage_info(PG_FUNCTION_ARGS)
3354
{
3455
bytea*raw_page=PG_GETARG_BYTEA_P(0);
35-
intraw_page_size;
3656
TupleDesctupdesc;
3757
Pagepage;
3858
GinPageOpaqueopaq;
@@ -46,12 +66,7 @@ gin_metapage_info(PG_FUNCTION_ARGS)
4666
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
4767
(errmsg("must be superuser to use raw page functions"))));
4868

49-
raw_page_size=VARSIZE(raw_page)-VARHDRSZ;
50-
if (raw_page_size<BLCKSZ)
51-
ereport(ERROR,
52-
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
53-
errmsg("input page too small (%d bytes)",raw_page_size)));
54-
page=VARDATA(raw_page);
69+
page=get_page_from_raw(raw_page);
5570

5671
opaq= (GinPageOpaque)PageGetSpecialPointer(page);
5772
if (opaq->flags!=GIN_META)
@@ -94,7 +109,6 @@ Datum
94109
gin_page_opaque_info(PG_FUNCTION_ARGS)
95110
{
96111
bytea*raw_page=PG_GETARG_BYTEA_P(0);
97-
intraw_page_size;
98112
TupleDesctupdesc;
99113
Pagepage;
100114
GinPageOpaqueopaq;
@@ -110,12 +124,7 @@ gin_page_opaque_info(PG_FUNCTION_ARGS)
110124
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
111125
(errmsg("must be superuser to use raw page functions"))));
112126

113-
raw_page_size=VARSIZE(raw_page)-VARHDRSZ;
114-
if (raw_page_size<BLCKSZ)
115-
ereport(ERROR,
116-
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
117-
errmsg("input page too small (%d bytes)",raw_page_size)));
118-
page=VARDATA(raw_page);
127+
page=get_page_from_raw(raw_page);
119128

120129
opaq= (GinPageOpaque)PageGetSpecialPointer(page);
121130

@@ -173,7 +182,6 @@ Datum
173182
gin_leafpage_items(PG_FUNCTION_ARGS)
174183
{
175184
bytea*raw_page=PG_GETARG_BYTEA_P(0);
176-
intraw_page_size;
177185
FuncCallContext*fctx;
178186
gin_leafpage_items_state*inter_call_data;
179187

@@ -182,20 +190,14 @@ gin_leafpage_items(PG_FUNCTION_ARGS)
182190
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
183191
(errmsg("must be superuser to use raw page functions"))));
184192

185-
raw_page_size=VARSIZE(raw_page)-VARHDRSZ;
186-
187193
if (SRF_IS_FIRSTCALL())
188194
{
189195
TupleDesctupdesc;
190196
MemoryContextmctx;
191197
Pagepage;
192198
GinPageOpaqueopaq;
193199

194-
if (raw_page_size<BLCKSZ)
195-
ereport(ERROR,
196-
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
197-
errmsg("input page too small (%d bytes)",raw_page_size)));
198-
page=VARDATA(raw_page);
200+
page=get_page_from_raw(raw_page);
199201

200202
if (PageGetSpecialSize(page)!=MAXALIGN(sizeof(GinPageOpaqueData)))
201203
ereport(ERROR,

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp