Skip to content

Commit

Permalink
Fixes for valgrind test
Browse files Browse the repository at this point in the history
  • Loading branch information
Zaryob committed Sep 17, 2024
1 parent 1547b73 commit d7032b8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
21 changes: 9 additions & 12 deletions src/dom.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,21 @@ struct dom_data {
static int
tagHook (struct dom_data *data, char *name, char **atts, int type)
{
iks *x;
ikstack *s = NULL; // Initialize s to NULL
iks* x;

if (IKS_OPEN == type || IKS_SINGLE == type) {
if (data->current) {
x = iks_insert(data->current, name);
} else {
s = iks_stack_new(data->chunk_size, data->chunk_size); // Allocate stack
}
else {
ikstack* s;
s = iks_stack_new(data->chunk_size, data->chunk_size);
x = iks_new_within(name, s);
if (!x) {
iks_free(s); // Free stack if memory allocation for x fails
iks_stack_delete (s);
return IKS_NOMEM;
}
}

if (atts) {
int i = 0;
while (atts[i]) {
Expand All @@ -41,16 +41,12 @@ tagHook (struct dom_data *data, char *name, char **atts, int type)
data->current = x;
}
if (IKS_CLOSE == type || IKS_SINGLE == type) {
x = iks_parent (data->current);

if (iks_strcmp(iks_name(data->current), name) != 0) {
if (s) iks_free(s); // Free the stack if comparison fails
x = iks_parent(data->current);
if (iks_strcmp(iks_name(data->current), name) != 0)
return IKS_BADXML;
}
if (x)
data->current = x;
else {
if (s) iks_free(s); // Free the stack when finished
*(data->iksptr) = data->current;
data->current = NULL;
}
Expand Down Expand Up @@ -150,6 +146,7 @@ iks_load (const char *fname, iks **xptr)
int e;
e = iks_parse (prs, buf, len, done);
if (IKS_OK != e) {
iks_parser_delete (prs);
ret = e;
break;
}
Expand Down
5 changes: 5 additions & 0 deletions test/tst-dom.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ document (char *xml)
iks_parser_delete (p);
}

void finalize() {
if (my_x) iks_delete(my_x);
}

void
tag (char *name, ...)
{
Expand Down Expand Up @@ -162,5 +166,6 @@ int main (int argc, char *argv[])
tag ("test", 0);
string (buf);

finalize();
return 0;
}

0 comments on commit d7032b8

Please sign in to comment.