Skip to content

Commit 2cc9de4

Browse files
AdamZvarasedmicha
authored andcommitted
Modifier: fix creating new templates
When new template was found, old one was not freed. Did't update the template in record if template was found.
1 parent 18bf497 commit 2cc9de4

File tree

1 file changed

+27
-16
lines changed

1 file changed

+27
-16
lines changed

src/core/modifier.c

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -857,7 +857,9 @@ ipx_modifier_append(struct fds_drec *rec,
857857
*/
858858
struct tmplt_cmp_data {
859859
/** Template to compare with */
860-
struct fds_template *t2;
860+
const struct fds_template *t1;
861+
/** Template to compare with */
862+
const struct fds_template *t2;
861863
/** ID of matched template (or 0 if none matched) */
862864
int id;
863865
};
@@ -878,11 +880,12 @@ template_cmp(const struct fds_template *t1, void *data)
878880

879881
// +- 2 to skip first field (ID)
880882
if (!memcmp(t1->raw.data+2, cmp->t2->raw.data+2, cmp->t2->raw.length-2)) {
881-
// Templates are equal, return id in cmp_data structure and stop iteration
883+
// Templates are equal
882884
cmp->id = t1->id;
883-
return false;
885+
cmp->t1 = t1;
886+
return false; // stop iteration
884887
}
885-
return true;
888+
return true; // continue iteration
886889
}
887890

888891
/**
@@ -929,21 +932,23 @@ template_set_new_id(ipx_modifier_t *mod, struct fds_template *tmplt)
929932
* \return #FDS_ERR_NOMEM for memory allocation error
930933
*/
931934
static int
932-
template_store(ipx_modifier_t *mod, struct fds_template *template, ipx_msg_garbage_t **garbage)
935+
template_store(ipx_modifier_t *mod, struct fds_drec *rec, ipx_msg_garbage_t **garbage)
933936
{
934937
int rc;
938+
struct fds_template *template = (struct fds_template *) rec->tmplt;
935939

936940
// Check if template exists
937941
const fds_tsnapshot_t *snapshot;
938942
rc = fds_tmgr_snapshot_get(mod->curr_ctx->mgr, &snapshot);
939943
if (rc != FDS_OK) {
940944
return rc;
941945
}
942-
struct tmplt_cmp_data cdata = { .t2 = template, .id = 0 };
946+
struct tmplt_cmp_data cdata = { .t2 = rec->tmplt, .id = 0 };
943947
fds_tsnapshot_for(snapshot, &template_cmp, &cdata);
944948

945949
// Template already exists
946950
if (cdata.id != 0) {
951+
rec->tmplt = cdata.t1;
947952
return IPX_OK;
948953
}
949954

@@ -963,6 +968,17 @@ template_store(ipx_modifier_t *mod, struct fds_template *template, ipx_msg_garba
963968
return rc;
964969
}
965970

971+
// Update record snapshot
972+
const fds_tsnapshot_t *snap;
973+
rc = fds_tmgr_snapshot_get(mod->curr_ctx->mgr, &snap);
974+
if (rc) {
975+
return rc;
976+
}
977+
rec->snap = snap;
978+
979+
// Set definitions to new fields (preserve == true)
980+
fds_template_ies_define(template, mod->iemgr, true);
981+
966982
return template->id;
967983
}
968984

@@ -1018,7 +1034,7 @@ ipx_modifier_modify(ipx_modifier_t *mod, const struct fds_drec *rec, ipx_msg_gar
10181034
}
10191035

10201036
new_tmplt = (struct fds_template *) new_rec->tmplt;
1021-
rc = template_store(mod, new_tmplt, garbage);
1037+
rc = template_store(mod, new_rec, garbage);
10221038

10231039
if (rc < FDS_OK) {
10241040
// Something bad happened
@@ -1039,18 +1055,13 @@ ipx_modifier_modify(ipx_modifier_t *mod, const struct fds_drec *rec, ipx_msg_gar
10391055
}
10401056
return NULL;
10411057
} else if (rc > FDS_OK) {
1058+
// Added new template
10421059
MODIFIER_DEBUG(mod, "Added new template %" PRIu16 " from template %" PRIu16 "",
10431060
rc, rec->tmplt->id);
1061+
} else {
1062+
// Template was already found
1063+
fds_template_destroy(new_tmplt);
10441064
}
10451065

1046-
// Update record snapshot
1047-
const fds_tsnapshot_t *snap;
1048-
rc = fds_tmgr_snapshot_get(mod->curr_ctx->mgr, &snap);
1049-
MODIFIER_ERR_CHECK(rc, new_rec, new_rec->data, new_tmplt);
1050-
new_rec->snap = snap;
1051-
1052-
// Set definitions to new fields (preserve == true)
1053-
fds_template_ies_define(new_tmplt, mod->iemgr, true);
1054-
10551066
return new_rec;
10561067
}

0 commit comments

Comments
 (0)