Fix transformJsonBehavior
authorAndrew Dunstan <[email protected]>
Thu, 14 Apr 2022 12:57:09 +0000 (08:57 -0400)
committerAndrew Dunstan <[email protected]>
Thu, 14 Apr 2022 13:24:22 +0000 (09:24 -0400)
Commit 1a36bc9dba8 conained some logic that was a little opaque and
could have involved a NULL dereference, as complained about by Coverity.
Make the logic more transparent and in doing so avoid the NULL
dereference.

src/backend/parser/parse_expr.c

index 3cbd5161528e92794c70a7ce452c5c8154138b2c..2f8c5e63fe8297aa9736e15ed725d3e1a58379f5 100644 (file)
@@ -4072,13 +4072,15 @@ static JsonBehavior *
 transformJsonBehavior(ParseState *pstate, JsonBehavior *behavior,
                                          JsonBehaviorType default_behavior)
 {
-       JsonBehaviorType behavior_type;
-       Node       *default_expr;
-
-       behavior_type = behavior ? behavior->btype : default_behavior;
-       default_expr = behavior_type != JSON_BEHAVIOR_DEFAULT ? NULL :
-               transformExprRecurse(pstate, behavior->default_expr);
+       JsonBehaviorType behavior_type = default_behavior;
+       Node       *default_expr = NULL;
 
+       if (behavior)
+       {
+               behavior_type = behavior->btype;
+               if (behavior_type == JSON_BEHAVIOR_DEFAULT)
+                       default_expr = transformExprRecurse(pstate, behavior->default_expr);
+       }
        return makeJsonBehavior(behavior_type, default_expr);
 }