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

Commit4cb8246

Browse files
committed
Cast result of copyObject() to correct type
copyObject() is declared to return void *, which allows easily assigningthe result independent of the input, but it loses all type checking.If the compiler supports typeof or something similar, cast the result tothe input type. This creates a greater amount of type safety. In somecases, where the result is assigned to a generic type such as Node * orExpr *, new casts are now necessary, but in general casts are nowunnecessary in the normal case and indicate that something unusual ishappening.Reviewed-by: Mark Dilger <hornschnorter@gmail.com>
1 parent66b7643 commit4cb8246

36 files changed

+178
-92
lines changed

‎config/c-compiler.m4

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,33 @@ fi])# PGAC_C_STATIC_ASSERT
178178

179179

180180

181+
# PGAC_C_TYPEOF
182+
# -------------
183+
# Check if the C compiler understands typeof or a variant. Define
184+
# HAVE_TYPEOF if so, and define 'typeof' to the actual key word.
185+
#
186+
AC_DEFUN([PGAC_C_TYPEOF],
187+
[AC_CACHE_CHECK(fortypeof,pgac_cv_c_typeof,
188+
[pgac_cv_c_typeof=no
189+
for pgac_kw in typeof __typeof__ decltype; do
190+
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([],
191+
[int x = 0;
192+
$pgac_kw(x) y;
193+
y = x;
194+
return y;])],
195+
[pgac_cv_c_typeof=$pgac_kw])
196+
test "$pgac_cv_c_typeof" != no && break
197+
done])
198+
if test "$pgac_cv_c_typeof" != no; then
199+
AC_DEFINE(HAVE_TYPEOF,1,
200+
[Define to 1 if your compiler understands `typeof' or something similar.])
201+
if test "$pgac_cv_c_typeof" != typeof; then
202+
AC_DEFINE(typeof,$pgac_cv_c_typeof,[Define to how the compiler spells `typeof'.])
203+
fi
204+
fi])# PGAC_C_TYPEOF
205+
206+
207+
181208
# PGAC_C_TYPES_COMPATIBLE
182209
# -----------------------
183210
# Check if the C compiler understands __builtin_types_compatible_p,

‎configure

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11667,6 +11667,46 @@ if test x"$pgac_cv__static_assert" = xyes ; then
1166711667

1166811668
$as_echo"#define HAVE__STATIC_ASSERT 1">>confdefs.h
1166911669

11670+
fi
11671+
{$as_echo"$as_me:${as_lineno-$LINENO}: checking for typeof">&5
11672+
$as_echo_n"checking for typeof...">&6; }
11673+
if${pgac_cv_c_typeof+:}false;then:
11674+
$as_echo_n"(cached)">&6
11675+
else
11676+
pgac_cv_c_typeof=no
11677+
forpgac_kwin typeof __typeof__ decltype;do
11678+
cat confdefs.h -<<_ACEOF >conftest.$ac_ext
11679+
/* end confdefs.h. */
11680+
11681+
int
11682+
main ()
11683+
{
11684+
int x = 0;
11685+
$pgac_kw(x) y;
11686+
y = x;
11687+
return y;
11688+
;
11689+
return 0;
11690+
}
11691+
_ACEOF
11692+
if ac_fn_c_try_compile"$LINENO";then:
11693+
pgac_cv_c_typeof=$pgac_kw
11694+
fi
11695+
rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
11696+
test"$pgac_cv_c_typeof"!= no&&break
11697+
done
11698+
fi
11699+
{$as_echo"$as_me:${as_lineno-$LINENO}: result:$pgac_cv_c_typeof">&5
11700+
$as_echo"$pgac_cv_c_typeof">&6; }
11701+
iftest"$pgac_cv_c_typeof"!= no;then
11702+
11703+
$as_echo"#define HAVE_TYPEOF 1">>confdefs.h
11704+
11705+
iftest"$pgac_cv_c_typeof"!= typeof;then
11706+
11707+
$as_echo"#define typeof\$pgac_cv_c_typeof">>confdefs.h
11708+
11709+
fi
1167011710
fi
1167111711
{$as_echo"$as_me:${as_lineno-$LINENO}: checking for __builtin_types_compatible_p">&5
1167211712
$as_echo_n"checking for __builtin_types_compatible_p...">&6; }

‎configure.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1330,6 +1330,7 @@ AC_C_FLEXIBLE_ARRAY_MEMBER
13301330
PGAC_C_SIGNED
13311331
PGAC_C_FUNCNAME_SUPPORT
13321332
PGAC_C_STATIC_ASSERT
1333+
PGAC_C_TYPEOF
13331334
PGAC_C_TYPES_COMPATIBLE
13341335
PGAC_C_BUILTIN_BSWAP32
13351336
PGAC_C_BUILTIN_BSWAP64

‎src/backend/bootstrap/bootstrap.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,11 +1082,11 @@ index_register(Oid heap,
10821082

10831083
memcpy(newind->il_info,indexInfo,sizeof(IndexInfo));
10841084
/* expressions will likely be null, but may as well copy it */
1085-
newind->il_info->ii_Expressions= (List*)
1085+
newind->il_info->ii_Expressions=
10861086
copyObject(indexInfo->ii_Expressions);
10871087
newind->il_info->ii_ExpressionsState=NIL;
10881088
/* predicate will likely be null, but may as well copy it */
1089-
newind->il_info->ii_Predicate= (List*)
1089+
newind->il_info->ii_Predicate=
10901090
copyObject(indexInfo->ii_Predicate);
10911091
newind->il_info->ii_PredicateState=NULL;
10921092
/* no exclusion constraints at bootstrap time, so no need to copy */

‎src/backend/commands/copy.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1470,7 +1470,7 @@ BeginCopy(ParseState *pstate,
14701470
* function and is executed repeatedly. (See also the same hack in
14711471
* DECLARE CURSOR and PREPARE.) XXX FIXME someday.
14721472
*/
1473-
rewritten=pg_analyze_and_rewrite((RawStmt*)copyObject(raw_query),
1473+
rewritten=pg_analyze_and_rewrite(copyObject(raw_query),
14741474
pstate->p_sourcetext,NULL,0);
14751475

14761476
/* check that we got back something we can work with */

‎src/backend/commands/createas.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ ExecCreateTableAs(CreateTableAsStmt *stmt, const char *queryString,
315315
* and is executed repeatedly. (See also the same hack in EXPLAIN and
316316
* PREPARE.)
317317
*/
318-
rewritten=QueryRewrite((Query*)copyObject(query));
318+
rewritten=QueryRewrite(copyObject(query));
319319

320320
/* SELECT should never rewrite to more or less than one SELECT query */
321321
if (list_length(rewritten)!=1)

‎src/backend/commands/event_trigger.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1869,7 +1869,7 @@ EventTriggerCollectAlterOpFam(AlterOpFamilyStmt *stmt, Oid opfamoid,
18691869
OperatorFamilyRelationId,opfamoid);
18701870
command->d.opfam.operators=operators;
18711871
command->d.opfam.procedures=procedures;
1872-
command->parsetree=copyObject(stmt);
1872+
command->parsetree=(Node*)copyObject(stmt);
18731873

18741874
currentEventTriggerState->commandList=
18751875
lappend(currentEventTriggerState->commandList,command);
@@ -1902,7 +1902,7 @@ EventTriggerCollectCreateOpClass(CreateOpClassStmt *stmt, Oid opcoid,
19021902
OperatorClassRelationId,opcoid);
19031903
command->d.createopc.operators=operators;
19041904
command->d.createopc.procedures=procedures;
1905-
command->parsetree=copyObject(stmt);
1905+
command->parsetree=(Node*)copyObject(stmt);
19061906

19071907
currentEventTriggerState->commandList=
19081908
lappend(currentEventTriggerState->commandList,command);
@@ -1937,7 +1937,7 @@ EventTriggerCollectAlterTSConfig(AlterTSConfigurationStmt *stmt, Oid cfgId,
19371937
command->d.atscfg.dictIds=palloc(sizeof(Oid)*ndicts);
19381938
memcpy(command->d.atscfg.dictIds,dictIds,sizeof(Oid)*ndicts);
19391939
command->d.atscfg.ndicts=ndicts;
1940-
command->parsetree=copyObject(stmt);
1940+
command->parsetree=(Node*)copyObject(stmt);
19411941

19421942
currentEventTriggerState->commandList=
19431943
lappend(currentEventTriggerState->commandList,command);
@@ -1967,7 +1967,7 @@ EventTriggerCollectAlterDefPrivs(AlterDefaultPrivilegesStmt *stmt)
19671967
command->type=SCT_AlterDefaultPrivileges;
19681968
command->d.defprivs.objtype=stmt->action->objtype;
19691969
command->in_extension=creating_extension;
1970-
command->parsetree=copyObject(stmt);
1970+
command->parsetree=(Node*)copyObject(stmt);
19711971

19721972
currentEventTriggerState->commandList=
19731973
lappend(currentEventTriggerState->commandList,command);

‎src/backend/commands/prepare.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ EvaluateParams(PreparedStatement *pstmt, List *params,
352352
* We have to run parse analysis for the expressions. Since the parser is
353353
* not cool about scribbling on its input, copy first.
354354
*/
355-
params=(List*)copyObject(params);
355+
params=copyObject(params);
356356

357357
pstate=make_parsestate(NULL);
358358
pstate->p_sourcetext=queryString;
@@ -554,7 +554,7 @@ FetchPreparedStatementTargetList(PreparedStatement *stmt)
554554
tlist=CachedPlanGetTargetList(stmt->plansource);
555555

556556
/* Copy into caller's context in case plan gets invalidated */
557-
return(List*)copyObject(tlist);
557+
returncopyObject(tlist);
558558
}
559559

560560
/*

‎src/backend/commands/view.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ UpdateRangeTableOfViewParse(Oid viewOid, Query *viewParse)
373373
* Var node twice. copyObject will expand any multiply-referenced subtree
374374
* into multiple copies.
375375
*/
376-
viewParse=(Query*)copyObject(viewParse);
376+
viewParse=copyObject(viewParse);
377377

378378
/* Create a dummy ParseState for addRangeTableEntryForRelation */
379379
pstate=make_parsestate(NULL);

‎src/backend/nodes/copyfuncs.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343

4444
/* Copy a field that is a pointer to some kind of Node or Node tree */
4545
#defineCOPY_NODE_FIELD(fldname) \
46-
(newnode->fldname =copyObject(from->fldname))
46+
(newnode->fldname =copyObjectImpl(from->fldname))
4747

4848
/* Copy a field that is a pointer to a Bitmapset */
4949
#defineCOPY_BITMAPSET_FIELD(fldname) \
@@ -4507,7 +4507,7 @@ _copyDropSubscriptionStmt(const DropSubscriptionStmt *from)
45074507
*/
45084508
#defineCOPY_NODE_CELL(new,old)\
45094509
(new) = (ListCell *) palloc(sizeof(ListCell));\
4510-
lfirst(new) =copyObject(lfirst(old));
4510+
lfirst(new) =copyObjectImpl(lfirst(old));
45114511

45124512
staticList*
45134513
_copyList(constList*from)
@@ -4610,13 +4610,13 @@ _copyForeignKeyCacheInfo(const ForeignKeyCacheInfo *from)
46104610

46114611

46124612
/*
4613-
* copyObject
4613+
*copyObjectImpl -- implementation ofcopyObject(); see nodes/nodes.h
46144614
*
46154615
* Create a copy of a Node tree or list. This is a "deep" copy: all
46164616
* substructure is copied too, recursively.
46174617
*/
46184618
void*
4619-
copyObject(constvoid*from)
4619+
copyObjectImpl(constvoid*from)
46204620
{
46214621
void*retval;
46224622

‎src/backend/optimizer/path/indxpath.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4004,9 +4004,9 @@ adjust_rowcompare_for_index(RowCompareExpr *clause,
40044004
matching_cols);
40054005
rc->inputcollids=list_truncate(list_copy(clause->inputcollids),
40064006
matching_cols);
4007-
rc->largs=list_truncate((List*)copyObject(clause->largs),
4007+
rc->largs=list_truncate(copyObject(clause->largs),
40084008
matching_cols);
4009-
rc->rargs=list_truncate((List*)copyObject(clause->rargs),
4009+
rc->rargs=list_truncate(copyObject(clause->rargs),
40104010
matching_cols);
40114011
return (Expr*)rc;
40124012
}

‎src/backend/optimizer/plan/createplan.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1275,7 +1275,7 @@ create_unique_plan(PlannerInfo *root, UniquePath *best_path, int flags)
12751275

12761276
foreach(l,uniq_exprs)
12771277
{
1278-
Node*uniqexpr=lfirst(l);
1278+
Expr*uniqexpr=lfirst(l);
12791279
TargetEntry*tle;
12801280

12811281
tle=tlist_member(uniqexpr,newtlist);
@@ -1318,7 +1318,7 @@ create_unique_plan(PlannerInfo *root, UniquePath *best_path, int flags)
13181318
groupColPos=0;
13191319
foreach(l,uniq_exprs)
13201320
{
1321-
Node*uniqexpr=lfirst(l);
1321+
Expr*uniqexpr=lfirst(l);
13221322
TargetEntry*tle;
13231323

13241324
tle=tlist_member(uniqexpr,newtlist);
@@ -4318,7 +4318,7 @@ process_subquery_nestloop_params(PlannerInfo *root, List *subplan_params)
43184318
/* No, so add it */
43194319
nlp=makeNode(NestLoopParam);
43204320
nlp->paramno=pitem->paramId;
4321-
nlp->paramval=copyObject(phv);
4321+
nlp->paramval=(Var*)copyObject(phv);
43224322
root->curOuterParams=lappend(root->curOuterParams,nlp);
43234323
}
43244324
}

‎src/backend/optimizer/plan/initsplan.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2306,8 +2306,8 @@ process_implied_equality(PlannerInfo *root,
23062306
clause=make_opclause(opno,
23072307
BOOLOID,/* opresulttype */
23082308
false,/* opretset */
2309-
(Expr*)copyObject(item1),
2310-
(Expr*)copyObject(item2),
2309+
copyObject(item1),
2310+
copyObject(item2),
23112311
InvalidOid,
23122312
collation);
23132313

@@ -2369,8 +2369,8 @@ build_implied_join_equality(Oid opno,
23692369
clause=make_opclause(opno,
23702370
BOOLOID,/* opresulttype */
23712371
false,/* opretset */
2372-
(Expr*)copyObject(item1),
2373-
(Expr*)copyObject(item2),
2372+
copyObject(item1),
2373+
copyObject(item2),
23742374
InvalidOid,
23752375
collation);
23762376

‎src/backend/optimizer/plan/planagg.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,11 +369,11 @@ build_minmax_path(PlannerInfo *root, MinMaxAggInfo *mminfo,
369369
subroot->outer_params=NULL;
370370
subroot->init_plans=NIL;
371371

372-
subroot->parse=parse=(Query*)copyObject(root->parse);
372+
subroot->parse=parse=copyObject(root->parse);
373373
IncrementVarSublevelsUp((Node*)parse,1,1);
374374

375375
/* append_rel_list might contain outer Vars? */
376-
subroot->append_rel_list=(List*)copyObject(root->append_rel_list);
376+
subroot->append_rel_list=copyObject(root->append_rel_list);
377377
IncrementVarSublevelsUp((Node*)subroot->append_rel_list,1,1);
378378
/* There shouldn't be any OJ info to translate, as yet */
379379
Assert(subroot->join_info_list==NIL);

‎src/backend/optimizer/plan/planner.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,7 +1157,7 @@ inheritance_planner(PlannerInfo *root)
11571157
* executor doesn't need to see the modified copies --- we can just
11581158
* pass it the original rowMarks list.)
11591159
*/
1160-
subroot->rowMarks=(List*)copyObject(root->rowMarks);
1160+
subroot->rowMarks=copyObject(root->rowMarks);
11611161

11621162
/*
11631163
* The append_rel_list likewise might contain references to subquery
@@ -1179,7 +1179,7 @@ inheritance_planner(PlannerInfo *root)
11791179
AppendRelInfo*appinfo2= (AppendRelInfo*)lfirst(lc2);
11801180

11811181
if (bms_is_member(appinfo2->child_relid,modifiableARIindexes))
1182-
appinfo2=(AppendRelInfo*)copyObject(appinfo2);
1182+
appinfo2=copyObject(appinfo2);
11831183

11841184
subroot->append_rel_list=lappend(subroot->append_rel_list,
11851185
appinfo2);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp