Skip to content

Commit

Permalink
Refactor: relax match in take_obj_notnull.cocci to catch more cases
Browse files Browse the repository at this point in the history
We have some places in the code where the verbatim "take obj" semantics do not
have setting the pointer to NULL immediately following.
  • Loading branch information
nigoroll committed Oct 27, 2024
1 parent 52a6d38 commit d5c981a
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 12 deletions.
3 changes: 1 addition & 2 deletions bin/varnishd/cache/cache_gzip.c
Original file line number Diff line number Diff line change
Expand Up @@ -345,10 +345,9 @@ vdp_gunzip_fini(struct vdp_ctx *vdc, void **priv)
struct vgz *vg;

(void)vdc;
CAST_OBJ_NOTNULL(vg, *priv, VGZ_MAGIC);
TAKE_OBJ_NOTNULL(vg, priv, VGZ_MAGIC);
AN(vg->m_buf);
(void)VGZ_Destroy(&vg);
*priv = NULL;
return (0);
}

Expand Down
4 changes: 2 additions & 2 deletions bin/varnishd/cache/cache_range.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ vrg_range_fini(struct vdp_ctx *vdc, void **priv)
struct vrg_priv *vrg_priv;

CHECK_OBJ_NOTNULL(vdc, VDP_CTX_MAGIC);
CAST_OBJ_NOTNULL(vrg_priv, *priv, VRG_PRIV_MAGIC);
TAKE_OBJ_NOTNULL(vrg_priv, priv, VRG_PRIV_MAGIC);
if (vrg_priv->req->resp_len >= 0 &&
vrg_priv->range_off < vrg_priv->range_high) {
Req_Fail(vrg_priv->req, SC_RANGE_SHORT);
vrg_priv->req->vdc->retval = -1;
}
*priv = NULL; /* struct on ws, no need to free */
/* struct on ws, no need to free */
return (0);
}

Expand Down
6 changes: 4 additions & 2 deletions tools/coccinelle/take_obj_notnull.cocci
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,15 @@ expression obj, priv, magic;
@@

- CAST_OBJ_NOTNULL(obj, *priv, magic);
- *priv = NULL;
+ TAKE_OBJ_NOTNULL(obj, priv, magic);
...
- *priv = NULL;

@@
expression obj, priv, magic;
@@

- CAST_OBJ_NOTNULL(obj, priv, magic);
- priv = NULL;
+ TAKE_OBJ_NOTNULL(obj, &priv, magic);
...
- priv = NULL;
9 changes: 3 additions & 6 deletions vmod/vmod_debug_filters.c
Original file line number Diff line number Diff line change
Expand Up @@ -292,11 +292,10 @@ xyzzy_pedantic_fini(struct vdp_ctx *vdc, void **priv)
AN(priv);
if (*priv == NULL)
return (0);
CAST_OBJ_NOTNULL(vdps, *priv, VDP_STATE_MAGIC);
TAKE_OBJ_NOTNULL(vdps, priv, VDP_STATE_MAGIC);
assert(vdps->state == VDPS_INIT || vdps->state == VDPS_END);
vdps->state = VDPS_FINI;

*priv = NULL;
return (0);
}

Expand Down Expand Up @@ -497,8 +496,7 @@ xyzzy_chksha256_fini(struct vdp_ctx *vdc, void **priv)
AN(priv);
if (*priv == NULL)
return (0);
CAST_OBJ_NOTNULL(vdps, *priv, VDP_CHKSHA256_MAGIC);
*priv = NULL;
TAKE_OBJ_NOTNULL(vdps, priv, VDP_CHKSHA256_MAGIC);

VSHA256_Final(digest, vdps->cx);
r = memcmp(digest, vdps->cfg->expected, sizeof digest);
Expand Down Expand Up @@ -542,8 +540,7 @@ xyzzy_chkcrc32_fini(struct vdp_ctx *vdc, void **priv)
AN(priv);
if (*priv == NULL)
return (0);
CAST_OBJ_NOTNULL(vdps, *priv, VDP_CHKCRC32_MAGIC);
*priv = NULL;
TAKE_OBJ_NOTNULL(vdps, priv, VDP_CHKCRC32_MAGIC);

if (vdps->crc == vdps->cfg->expected)
return (0);
Expand Down

0 comments on commit d5c981a

Please sign in to comment.