Skip to content

Commit 18bf497

Browse files
AdamZvarasedmicha
authored andcommitted
Modifier: fix generating template ID
Template ID is now generated only with new generated template. Fixed memory leaks in modifier context tests.
1 parent 0124728 commit 18bf497

File tree

2 files changed

+43
-42
lines changed

2 files changed

+43
-42
lines changed

src/core/modifier.c

Lines changed: 40 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,32 @@ template_cmp(const struct fds_template *t1, void *data)
885885
return true;
886886
}
887887

888+
/**
889+
* \brief Generate and set new ID for template
890+
*
891+
* \param[in] mod Modifier
892+
* \param[in] tmplt Modified template
893+
* \return #IPX_OK on success, #IPX_ERR_LIMIT when maximum number of template IDs has been reached
894+
*/
895+
static int
896+
template_set_new_id(ipx_modifier_t *mod, struct fds_template *tmplt)
897+
{
898+
uint16_t new_id = mod->curr_ctx->next_id;
899+
900+
if (new_id == 0) {
901+
// No more IDs are availiable!
902+
MODIFIER_WARNING(mod, "No more IDs availiable for session %s (ODID: %d)",
903+
mod->curr_ctx->session->ident, mod->curr_ctx->odid);
904+
return IPX_ERR_LIMIT;
905+
}
906+
907+
tmplt->id = new_id;
908+
uint16_t *raw_id = (uint16_t *) tmplt->raw.data;
909+
*raw_id = htons(new_id);
910+
mod->curr_ctx->next_id = ++new_id;
911+
return IPX_OK;
912+
}
913+
888914
/**
889915
* \brief Search for template in manager, add new if it does not exist
890916
*
@@ -903,13 +929,13 @@ template_cmp(const struct fds_template *t1, void *data)
903929
* \return #FDS_ERR_NOMEM for memory allocation error
904930
*/
905931
static int
906-
template_store(fds_tmgr_t *mgr, struct fds_template *template)
932+
template_store(ipx_modifier_t *mod, struct fds_template *template, ipx_msg_garbage_t **garbage)
907933
{
908934
int rc;
909935

910936
// Check if template exists
911937
const fds_tsnapshot_t *snapshot;
912-
rc = fds_tmgr_snapshot_get(mgr, &snapshot);
938+
rc = fds_tmgr_snapshot_get(mod->curr_ctx->mgr, &snapshot);
913939
if (rc != FDS_OK) {
914940
return rc;
915941
}
@@ -921,41 +947,25 @@ template_store(fds_tmgr_t *mgr, struct fds_template *template)
921947
return IPX_OK;
922948
}
923949

950+
// Try to create new ID for template
951+
rc = template_set_new_id(mod, template);
952+
if (rc == IPX_ERR_LIMIT) {
953+
// No more IDs available, restart context
954+
modifier_ctx_restart(mod, garbage);
955+
// Need to set new ID again
956+
rc = template_set_new_id(mod, template);
957+
assert(rc == IPX_OK);
958+
}
959+
924960
// Try to add template
925-
rc = fds_tmgr_template_add(mgr, template);
961+
rc = fds_tmgr_template_add(mod->curr_ctx->mgr, template);
926962
if (rc != FDS_OK) {
927963
return rc;
928964
}
929965

930966
return template->id;
931967
}
932968

933-
/**
934-
* \brief Generate and set new ID for template
935-
*
936-
* \param[in] mod Modifier
937-
* \param[in] tmplt Modified template
938-
* \return #IPX_OK on success, #IPX_ERR_LIMIT when maximum number of template IDs has been reached
939-
*/
940-
static int
941-
template_set_new_id(ipx_modifier_t *mod, struct fds_template *tmplt)
942-
{
943-
uint16_t new_id = mod->curr_ctx->next_id;
944-
945-
if (new_id == 0) {
946-
// No more IDs are availiable!
947-
MODIFIER_WARNING(mod, "No more IDs availiable for session %s (ODID: %d)",
948-
mod->curr_ctx->session->ident, mod->curr_ctx->odid);
949-
return IPX_ERR_LIMIT;
950-
}
951-
952-
tmplt->id = new_id;
953-
uint16_t *raw_id = (uint16_t *) tmplt->raw.data;
954-
*raw_id = htons(new_id);
955-
mod->curr_ctx->next_id = ++new_id;
956-
return IPX_OK;
957-
}
958-
959969
struct fds_drec *
960970
ipx_modifier_modify(ipx_modifier_t *mod, const struct fds_drec *rec, ipx_msg_garbage_t **garbage)
961971
{
@@ -1008,18 +1018,7 @@ ipx_modifier_modify(ipx_modifier_t *mod, const struct fds_drec *rec, ipx_msg_gar
10081018
}
10091019

10101020
new_tmplt = (struct fds_template *) new_rec->tmplt;
1011-
1012-
// Try to create new ID for template
1013-
rc = template_set_new_id(mod, new_tmplt);
1014-
if (rc == IPX_ERR_LIMIT) {
1015-
// No more IDs available, restart context
1016-
modifier_ctx_restart(mod, garbage);
1017-
// Need to set new ID again
1018-
rc = template_set_new_id(mod, new_tmplt);
1019-
assert(rc == IPX_OK);
1020-
}
1021-
1022-
rc = template_store(mod->curr_ctx->mgr, new_tmplt);
1021+
rc = template_store(mod, new_tmplt, garbage);
10231022

10241023
if (rc < FDS_OK) {
10251024
// Something bad happened

tests/unit/core/modifier/modifier_context.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,13 +513,15 @@ TEST_F(ErrorSession, deleteNonExisting) {
513513
Session *sessions = createSessions("deleteEmpty");
514514
ipx_msg_garbage_t *gb_msg;
515515
EXPECT_EQ(ipx_modifier_remove_session(mod, sessions->getSession(FDS_SESSION_FILE), &gb_msg), IPX_ERR_NOTFOUND);
516+
delete sessions;
516517
}
517518

518519
/** Attempt to delete non existing session */
519520
TEST_F(ErrorSession, TCPSetTimeInHistory) {
520521
Session *sessions = createSessions("test");
521522

522523
ipx_session *session = sessions->getSession(FDS_SESSION_TCP);
523-
addSession(session, 0 ,100);
524+
addSession(session, 0 , 100);
524525
addSession(session, 0, 10, IPX_ERR_FORMAT);
526+
delete sessions;
525527
}

0 commit comments

Comments
 (0)