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

Commitd4d441d

Browse files
author
Karina Litskevich
committed
[PGPRO-10079] Use StringInfo instead of fix-sized buffers
This way we don't worry about allocating enough memory as appendStringInfodoes it for usTags: vops
1 parent8348d48 commitd4d441d

File tree

2 files changed

+19
-19
lines changed

2 files changed

+19
-19
lines changed

‎vops.c

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -823,23 +823,23 @@ static bool is_vops_type(Oid typeid)
823823
Datum vops_##TYPE##_output(PG_FUNCTION_ARGS)\
824824
{\
825825
vops_##TYPE* tile = (vops_##TYPE*)PG_GETARG_POINTER(0);\
826-
char buf[MAX_TILE_STRLEN]; \
827-
int p = 0;\
826+
StringInfoData str;\
828827
char sep = '{';\
829828
int i;\
829+
initStringInfo(&str);\
830830
for (i = 0; i < TILE_SIZE; i++) {\
831831
if (tile->hdr.empty_mask & ((uint64)1 << i)) {\
832-
p += sprintf(buf + p, "%c", sep);\
832+
appendStringInfo(&str, "%c", sep);\
833833
} else if (tile->hdr.null_mask & ((uint64)1 << i)) {\
834-
p += sprintf(buf + p, "%c?", sep);\
834+
appendStringInfo(&str, "%c?", sep);\
835835
} else {\
836-
p += sprintf(buf + p, "%c%.*" #FORMAT, sep, PREC, (STYPE)tile->payload[i]); \
836+
appendStringInfo(&str, "%c%.*" #FORMAT, sep, PREC, (STYPE)tile->payload[i]); \
837837
}\
838838
sep = ',';\
839839
}\
840-
strcpy(buf + p, "}");\
841-
PG_RETURN_CSTRING(pstrdup(buf));\
842-
}
840+
appendStringInfo(&str, "}");\
841+
PG_RETURN_CSTRING(str.data);\
842+
}
843843

844844

845845

@@ -2497,7 +2497,7 @@ Datum vops_populate(PG_FUNCTION_ARGS)
24972497
char*sql;
24982498
charsep;
24992499
TupleDescspi_tupdesc;
2500-
inti,j,n,n_attrs;
2500+
inti,j,n_attrs;
25012501
vops_type_info*types;
25022502
Datum*values;
25032503
bool*nulls;
@@ -2508,7 +2508,7 @@ Datum vops_populate(PG_FUNCTION_ARGS)
25082508
int64loaded;
25092509
booltype_checked= false;
25102510
staticOidself_oid=InvalidOid;
2511-
charstmt[MAX_SQL_STMT_LEN];
2511+
StringInfoDatastmt;
25122512

25132513
/* Detect case when extension is drop and created several times */
25142514
if (fcinfo->flinfo->fn_oid!=self_oid)
@@ -2533,7 +2533,8 @@ Datum vops_populate(PG_FUNCTION_ARGS)
25332533
values= (Datum*)palloc(sizeof(Datum)*n_attrs);
25342534
nulls= (bool*)palloc0(sizeof(bool)*n_attrs);
25352535

2536-
n=sprintf(stmt,"select");
2536+
initStringInfo(&stmt);
2537+
appendStringInfo(&stmt,"select");
25372538
sep=' ';
25382539
spi_tupdesc=SPI_tuptable->tupdesc;
25392540
for (i=0;i<n_attrs;i++) {
@@ -2549,25 +2550,26 @@ Datum vops_populate(PG_FUNCTION_ARGS)
25492550
elog(ERROR,"Size of column %s is unknown",name);
25502551
}
25512552
}
2552-
n+=sprintf(stmt+n,"%c%s",sep,name);
2553+
appendStringInfo(&stmt,"%c%s",sep,name);
25532554
sep=',';
25542555
SPI_freetuple(spi_tuple);
25552556
}
25562557
SPI_freetuptable(SPI_tuptable);
25572558

2558-
n+=sprintf(stmt+n," from %s.%s",
2559+
appendStringInfo(&stmt," from %s.%s",
25592560
get_namespace_name(get_rel_namespace(source)),
25602561
get_rel_name(source));
25612562
if (predicate&&*predicate) {
2562-
n+=sprintf(stmt+n," where %s",predicate);
2563+
appendStringInfo(&stmt," where %s",predicate);
25632564
}
25642565
if (sort&&*sort) {
2565-
n+=sprintf(stmt+n," order by %s",sort);
2566+
appendStringInfo(&stmt," order by %s",sort);
25662567
}
2567-
plan=SPI_prepare(stmt,0,NULL);
2568+
plan=SPI_prepare(stmt.data,0,NULL);
25682569
if (plan==NULL)
25692570
elog(ERROR,"SPI_prepare(\"%s\") failed:%s",
2570-
stmt,SPI_result_code_string(SPI_result));
2571+
stmt.data,SPI_result_code_string(SPI_result));
2572+
pfree(stmt.data);
25712573
portal=SPI_cursor_open(NULL,plan,NULL,NULL, true);
25722574

25732575
begin_batch_insert(destination);

‎vops.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ typedef enum
3939

4040

4141
#defineTILE_SIZE 64/* just because of maximum size of bitmask */
42-
#defineMAX_SQL_STMT_LEN 1024
4342
#defineMAX_CSV_LINE_LEN 4096
44-
#defineMAX_TILE_STRLEN (TILE_SIZE*16)
4543
#defineINIT_MAP_SIZE (1024*1024)
4644

4745
typedeflong longlong64;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp