dfilter: Remove syntax node magic number

Syntax nodes always have one type and the compiler
can check it. Reserve magic numbers for data contained
inside syntax node. Remove the magic number field in
stnode_t and the STNODE_MAGIC define. Keep the other
magic values for type checking.

This exposed a bug with an assertion for oper type syntax
nodes. Fix the assertion to check the correct pointer value
against the magic number.
This commit is contained in:
João Valverde 2023-10-02 16:53:24 +01:00
parent c0885fe390
commit cd01931eb9
3 changed files with 5 additions and 25 deletions

View file

@ -337,8 +337,9 @@ sttype_oper_set_op(stnode_t *node, stnode_op_t op)
stnode_op_t
sttype_oper_get_op(stnode_t *node)
{
ws_assert_magic(node, OPER_MAGIC);
return ((oper_t *)node)->op;
oper_t *oper = stnode_data(node);
ws_assert_magic(oper, OPER_MAGIC);
return oper->op;
}
void

View file

@ -22,9 +22,6 @@
static sttype_t* type_list[STTYPE_NUM_TYPES];
#define STNODE_MAGIC 0xe9b00b9e
void
sttype_init(void)
{
@ -79,7 +76,6 @@ sttype_lookup(sttype_id_t type_id)
void
stnode_clear(stnode_t *node)
{
ws_assert_magic(node, STNODE_MAGIC);
if (node->type) {
if (node->type->func_free && node->data) {
node->type->func_free(node->data);
@ -107,7 +103,6 @@ stnode_init(stnode_t *node, sttype_id_t type_id, void *data, char *token, df_loc
{
sttype_t *type;
ws_assert_magic(node, STNODE_MAGIC);
ws_assert(!node->type);
ws_assert(!node->data);
node->repr_display = NULL;
@ -149,13 +144,8 @@ stnode_replace(stnode_t *node, sttype_id_t type_id, void *data)
stnode_t*
stnode_new(sttype_id_t type_id, void *data, char *token, df_loc_t loc)
{
stnode_t *node;
node = g_new0(stnode_t, 1);
node->magic = STNODE_MAGIC;
stnode_t *node = g_new0(stnode_t, 1);
stnode_init(node, type_id, data, token, loc);
return node;
}
@ -171,9 +161,7 @@ stnode_dup(const stnode_t *node)
{
stnode_t *new;
ws_assert_magic(node, STNODE_MAGIC);
new = g_new(stnode_t, 1);
new->magic = STNODE_MAGIC;
new->repr_display = NULL;
new->repr_debug = NULL;
new->repr_token = g_strdup(node->repr_token);
@ -194,7 +182,6 @@ stnode_dup(const stnode_t *node)
void
stnode_free(stnode_t *node)
{
ws_assert_magic(node, STNODE_MAGIC);
stnode_clear(node);
g_free(node);
}
@ -202,7 +189,6 @@ stnode_free(stnode_t *node)
const char*
stnode_type_name(stnode_t *node)
{
ws_assert_magic(node, STNODE_MAGIC);
if (node->type)
return node->type->name;
else
@ -212,7 +198,6 @@ stnode_type_name(stnode_t *node)
sttype_id_t
stnode_type_id(stnode_t *node)
{
ws_assert_magic(node, STNODE_MAGIC);
if (node->type)
return node->type->id;
else
@ -222,7 +207,6 @@ stnode_type_id(stnode_t *node)
void *
stnode_data(stnode_t *node)
{
ws_assert_magic(node, STNODE_MAGIC);
return node->data;
}
@ -236,7 +220,6 @@ stnode_string(stnode_t *node)
void *
stnode_steal_data(stnode_t *node)
{
ws_assert_magic(node, STNODE_MAGIC);
void *data = node->data;
ws_assert(data);
node->data = NULL;
@ -321,8 +304,6 @@ _node_tostr(stnode_t *node, bool pretty)
const char *
stnode_tostr(stnode_t *node, bool pretty)
{
ws_assert_magic(node, STNODE_MAGIC);
if (pretty && IS_OPERATOR(node) && node->repr_token != NULL) {
/* Some operators can have synonyms, like "or" and "||".
* Show the user the same representation as he typed. */
@ -351,7 +332,6 @@ sprint_node(stnode_t *node)
wmem_strbuf_t *buf = wmem_strbuf_new(NULL, NULL);
wmem_strbuf_append_printf(buf, "{ ");
wmem_strbuf_append_printf(buf, "magic = 0x%"PRIx32", ", node->magic);
wmem_strbuf_append_printf(buf, "type = %s, ", stnode_type_name(node));
wmem_strbuf_append_printf(buf, "data = %s, ", stnode_todebug(node));
wmem_strbuf_append_printf(buf, "location = %ld:%zu",

View file

@ -60,9 +60,8 @@ typedef struct {
/** Node (type instance) information */
typedef struct {
uint32_t magic;
sttype_t *type;
void * data;
void *data;
char *repr_token;
char *repr_display;
char *repr_debug;