Skip to content

Commit

Permalink
Regression test might be need to get valgrind test's analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
Zaryob committed Sep 16, 2024
1 parent 113d8af commit 08835d0
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 11 deletions.
23 changes: 16 additions & 7 deletions src/dom.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,40 @@ static int
tagHook (struct dom_data *data, char *name, char **atts, int type)
{
iks *x;
ikstack *s = NULL; // Initialize s to NULL

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

if (atts) {
int i=0;
int i = 0;
while (atts[i]) {
iks_insert_attrib (x, atts[i], atts[i+1]);
iks_insert_attrib(x, atts[i], atts[i + 1]);
i += 2;
}
}
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 (iks_strcmp(iks_name(data->current), name) != 0) {
if (s) iks_free(s); // Free the stack if comparison fails
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
5 changes: 4 additions & 1 deletion src/ikstack.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ iks_stack_new (size_t meta_chunk, size_t data_chunk)

len = sizeof (ikstack) + meta_chunk + data_chunk + (sizeof (ikschunk) * 2);
s = iks_malloc (len);
if (!s) return NULL;
if (!s) {
iks_free (s);
return NULL;
}
s->allocated = len;
s->meta = (ikschunk *) ((char *) s + sizeof (ikstack));
s->meta->next = NULL;
Expand Down
7 changes: 5 additions & 2 deletions src/sax.c
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,10 @@ sax_core (iksparser *prs, char *buf, int len)
char **tmp;
if (prs->attcur == 0) tmp = NULL; else tmp = prs->atts;
err = prs->tagHook (prs->user_data, prs->tag_name, tmp, prs->tagtype);
if (IKS_OK != err) return err;
if (IKS_OK != err) {
iks_free(prs->atts);
return err;
}
}
prs->stack_pos = 0;
stack_old = -1;
Expand Down Expand Up @@ -374,7 +377,7 @@ sax_core (iksparser *prs, char *buf, int len)
if (!tmp) return IKS_NOMEM;
memset (tmp, 0, sizeof(char *) * 2 * prs->attmax);
memcpy (tmp, prs->atts, sizeof(char *) * prs->attcur);
free (prs->atts);
iks_free(prs->atts);
prs->atts = tmp;
}
}
Expand Down
2 changes: 2 additions & 0 deletions test/tst-dom.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ document (char *xml)
break;
case IKS_NOMEM:
PR_TEST;
iks_parser_delete(p);
puts ("Not enough memory.");
exit (1);
case IKS_BADXML:
PR_TEST;
printf ("Invalid xml at byte %ld in\n[%s]\n", iks_nr_bytes (p), xml);
iks_parser_delete(p);
exit (1);
case IKS_HOOK:
PR_TEST;
Expand Down
11 changes: 10 additions & 1 deletion test/tst-sax.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,12 +230,15 @@ test_size (int blocksize)
case IKS_OK:
break;
case IKS_NOMEM:
iks_parser_delete(prs);
exit (1);
case IKS_BADXML:
PRINT_TEST;
printf ("Invalid xml at byte %ld in\n[%s]\n", iks_nr_bytes (prs), tester.doc);
iks_parser_delete(prs);
exit (1);
case IKS_HOOK:
iks_parser_delete(prs);
exit (1);
}
i += blocksize;
Expand Down Expand Up @@ -266,13 +269,19 @@ test_bad (int badbyte)
case IKS_OK:
break;
case IKS_NOMEM:
iks_parser_delete(p);
exit (1);
case IKS_BADXML:
if (iks_nr_bytes (p) == badbyte) return;
if (iks_nr_bytes (p) == badbyte) {
iks_parser_delete(p);
return;
}
break;
case IKS_HOOK:
iks_parser_delete(p);
exit (1);
}
iks_parser_delete(p);
printf ("Sax test %d:\n", tester.nr_tests);
printf ("Expected bad byte %d, got %ld in\n[%s]\n", badbyte, iks_nr_bytes (p), tester.doc);
exit (1);
Expand Down

0 comments on commit 08835d0

Please sign in to comment.