Fixed some major issues.

This commit includes the following changes:
- Fixed segfault after a caff parse results in NULL
- Fixed segfault when caff has no animation
- Fixed segfault when the animation header is smaller, than it needs to
- Added new testcases
- Better testing system

Signed-off-by: Balazs Toldi <balazs@toldi.eu>
This commit is contained in:
Balazs Toldi 2023-05-30 07:44:38 +02:00
parent 43a6108f89
commit 303a837467
Signed by: Bazsalanszky
GPG key ID: 6C7D440036F99D58
14 changed files with 38 additions and 16 deletions

1
.gitignore vendored
View file

@ -1,7 +1,6 @@
build/
parser
parser_test
*.caff
*.jpg
*.jpeg
.vscode

1
8.caff Normal file
View file

@ -0,0 +1 @@


10
caff.c
View file

@ -86,8 +86,10 @@ CAFF *caff_parse_file(const char *filename)
}
// You might want to append to a list of animations here.
CAFF_Animation *anim = read_animation(file);
caff_add_animation_list(&caff->animations, *anim);
if(anim){
caff_add_animation_list(&caff->animations, *anim);
}
free(anim);
// We only need the first image now...
fclose(file);
@ -96,6 +98,10 @@ CAFF *caff_parse_file(const char *filename)
// Random bytes should not cause an error
printf("Unknown block ID: %u\n", id);
fclose(file);
if(anim_count == 0){
free(caff);
caff = NULL;
}
return caff;
}
}

16
ciff.c
View file

@ -22,11 +22,17 @@ CIFF *read_ciff(FILE *file)
}
// Read the header
fread(ciff->magic, sizeof(uint32_t), 1, file);
fread(&ciff->header_size, sizeof(uint64_t), 1, file);
fread(&ciff->content_size, sizeof(uint64_t), 1, file);
fread(&ciff->width, sizeof(uint64_t), 1, file);
fread(&ciff->height, sizeof(uint64_t), 1, file);
if (
fread(ciff->magic, sizeof(uint32_t), 1, file) != 1 ||
fread(&ciff->header_size, sizeof(uint64_t), 1, file) != 1 ||
fread(&ciff->content_size, sizeof(uint64_t), 1, file) != 1 ||
fread(&ciff->width, sizeof(uint64_t), 1, file) != 1 ||
fread(&ciff->height, sizeof(uint64_t), 1, file) != 1 ) {
printf("Failed to read CIFF header!\n");
free(ciff);
fclose(file);
return NULL;
}
if (ciff->width * ciff->height*3 < ciff->content_size)
{
printf("w:%ld h:%ld s:%ld",ciff->width,ciff->height,ciff->content_size);

7
main.c
View file

@ -24,12 +24,14 @@ int main(int argc, char **argv)
strncpy(extension, ".jpeg", MAX_PATH_LEN - (extension - jpg_path));
}
printf("%s\n", jpg_path);
CIFF *ciff;
CIFF *ciff = NULL;
CAFF *caff = NULL;
if (is_caff)
{
caff = caff_parse_file(input_path);
ciff = caff->animations.array[0].ciff;
if(caff != NULL && caff->animations.length >= 1) {
ciff = caff->animations.array[0].ciff;
}
}
else
{
@ -52,5 +54,6 @@ int main(int argc, char **argv)
{
free_ciff(ciff);
}
return 0;
}

18
test.c
View file

@ -1,19 +1,25 @@
#include <stdio.h>
#include <stdlib.h>
#define TEST_COUNT 3
#define TEST_COUNT 8
#define COMMAND_SIZE 128
int main(void)
{
int failure = 0;
int res[TEST_COUNT];
for (size_t i = 1; i <= TEST_COUNT; i++)
{
char command[COMMAND_SIZE];
sprintf(command,"valgrind --track-origins=yes -s ./parser -caff %ld.caff",i);
int res = system(command);
printf("Test %ld result: %d (%s)\n",i, res, (res == 0) ? "SUCCESS" : "FAIL");
failure += (res != 0) ? 1 : 0;
sprintf(command,"valgrind --track-origins=yes -s ./parser -caff test_cases/%ld.caff",i);
res[i-1] = system(command);
printf("Test %ld result: %d (%s)\n",i, res[i-1], ( i > 3 ? res[i-1]== 65280 : res[i-1]== 0) ? "SUCCESS" : "FAIL");
failure += (res[i-1] != 0) ? 1 : 0;
}
printf("Summary:\n");
for (size_t i = 0; i < TEST_COUNT; i++)
{
printf("Test %ld result: %s\n",i+1, ( i > 2 ? res[i]== 65280 : res[i]== 0) ? "PASS" : "FAIL");
}
}

BIN
test_cases/1.caff Normal file

Binary file not shown.

BIN
test_cases/2.caff Normal file

Binary file not shown.

BIN
test_cases/3.caff Normal file

Binary file not shown.

BIN
test_cases/4.caff Normal file

Binary file not shown.

BIN
test_cases/5.caff Normal file

Binary file not shown.

BIN
test_cases/6.caff Normal file

Binary file not shown.

BIN
test_cases/7.caff Normal file

Binary file not shown.

1
test_cases/8.caff Normal file
View file

@ -0,0 +1 @@