commit 9c3e340f4be8cf2a87b1e9728b5f8e858a3052a1
parent 4f6e487209b76ef0b11f258f3a26b83a7379686b
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Tue, 3 Nov 2020 16:32:08 +0100
Test the loading of badly formatted file
Diffstat:
2 files changed, 129 insertions(+), 4 deletions(-)
diff --git a/src/atrck.c b/src/atrck.c
@@ -182,9 +182,13 @@ load_stream(struct atrck* atrck, FILE* stream, const char* stream_name)
#undef READ
/* Check band description */
- if(!IS_POW2(atrck->pagesize)) {
- log_err(atrck, "%s: invalid pagesize %lu.\n",
- stream_name, (unsigned long)atrck->pagesize);
+ if(!IS_ALIGNED(atrck->pagesize, atrck->pagesize_os)) {
+ log_err(atrck,
+ "%s: invalid page size %lu. The page size attribute must be aligned on "
+ "the page size of the operating system (%lu).\n",
+ stream_name,
+ (unsigned long)atrck->pagesize,
+ (unsigned long)atrck->pagesize_os);
res = RES_BAD_ARG;
goto error;
}
@@ -220,7 +224,8 @@ load_stream(struct atrck* atrck, FILE* stream, const char* stream_name)
map_len = ALIGN_SIZE(atrck->nnodes * sizeof(float), atrck->pagesize);
/* Compute the offset toward the 1st list of ka */
- offset = (off_t)ALIGN_SIZE((uint64_t)ftell(stream), atrck->pagesize);
+ offset = ftell(stream);
+ offset = (off_t)ALIGN_SIZE((uint64_t)offset, atrck->pagesize);
/* Load the per band, per quadrature point and per node correlated K */
FOR_EACH(iband, 0, nbands) {
diff --git a/src/test_atrck_load.c b/src/test_atrck_load.c
@@ -157,6 +157,125 @@ test_load(struct atrck* atrck)
}
static void
+test_load_fail(struct atrck* atrck)
+{
+ const char byte = 1;
+ FILE* fp = NULL;
+ uint64_t pagesize;
+ uint64_t nnodes;
+ uint64_t nbands;
+ uint64_t iband;
+ uint64_t nqpts;
+ double low;
+ double upp;
+ double abscissa;
+ double weight;
+ double ka;
+
+ /* Wrong pagesize */
+ fp = tmpfile();
+ CHK(fp);
+ pagesize = 2048;
+ nnodes = 1;
+ nbands = 1;
+ CHK(fwrite(&pagesize, sizeof(pagesize), 1, fp) == 1);
+ CHK(fwrite(&nbands, sizeof(nbands), 1, fp) == 1);
+ CHK(fwrite(&nnodes, sizeof(nnodes), 1, fp) == 1);
+ iband = 0;
+ low = 0;
+ upp = 1;
+ nqpts = 1;
+ CHK(fwrite(&iband, sizeof(iband), 1, fp) == 1);
+ CHK(fwrite(&low, sizeof(low), 1, fp) == 1);
+ CHK(fwrite(&upp, sizeof(upp), 1, fp) == 1);
+ CHK(fwrite(&nqpts, sizeof(nqpts), 1, fp) == 1);
+ abscissa = 1;
+ weight = 1;
+ CHK(fwrite(&abscissa, sizeof(abscissa), 1, fp) == 1);
+ CHK(fwrite(&weight, sizeof(weight), 1, fp) == 1);
+ CHK(fseek(fp, (long)ALIGN_SIZE((size_t)ftell(fp), pagesize), SEEK_SET) == 0);
+ ka = 1;
+ CHK(fwrite(&ka, sizeof(ka), 1, fp) == 1);
+ CHK(fseek(fp, (long)ALIGN_SIZE((size_t)ftell(fp), pagesize)-1, SEEK_SET) == 0);
+ CHK(fwrite(&byte, sizeof(byte), 1, fp) == 1); /* Positioned the EOF */
+ rewind(fp);
+
+ CHK(atrck_load_stream(atrck, fp, NULL) == RES_BAD_ARG);
+ fclose(fp);
+
+ /* Wrong #bands */
+ fp = tmpfile();
+ CHK(fp);
+ pagesize = 4096;
+ nnodes = 1;
+ nbands = 0;
+ CHK(fwrite(&pagesize, sizeof(pagesize), 1, fp) == 1);
+ CHK(fwrite(&nbands, sizeof(nbands), 1, fp) == 1);
+ CHK(fwrite(&nnodes, sizeof(nnodes), 1, fp) == 1);
+ rewind(fp);
+
+ CHK(atrck_load_stream(atrck, fp, NULL) == RES_BAD_ARG);
+ fclose(fp);
+
+ /* Wrong #nodes */
+ fp = tmpfile();
+ CHK(fp);
+ pagesize = 4096;
+ nnodes = 0;
+ nbands = 1;
+ CHK(fwrite(&pagesize, sizeof(pagesize), 1, fp) == 1);
+ CHK(fwrite(&nbands, sizeof(nbands), 1, fp) == 1);
+ CHK(fwrite(&nnodes, sizeof(nnodes), 1, fp) == 1);
+ iband = 0;
+ low = 0;
+ upp = 1;
+ nqpts = 1;
+ CHK(fwrite(&iband, sizeof(iband), 1, fp) == 1);
+ CHK(fwrite(&low, sizeof(low), 1, fp) == 1);
+ CHK(fwrite(&upp, sizeof(upp), 1, fp) == 1);
+ CHK(fwrite(&nqpts, sizeof(nqpts), 1, fp) == 1);
+ abscissa = 1;
+ weight = 1;
+ CHK(fwrite(&abscissa, sizeof(abscissa), 1, fp) == 1);
+ CHK(fwrite(&weight, sizeof(weight), 1, fp) == 1);
+ CHK(fseek(fp, (long)ALIGN_SIZE((size_t)ftell(fp), pagesize)-1, SEEK_SET) == 0);
+ CHK(fwrite(&byte, sizeof(byte), 1, fp) == 1); /* Positioned the EOF */
+ rewind(fp);
+
+ /* Wrong band boundaries */
+ fp = tmpfile();
+ CHK(fp);
+ pagesize = 16384;
+ nnodes = 1;
+ nbands = 1;
+ CHK(fwrite(&pagesize, sizeof(pagesize), 1, fp) == 1);
+ CHK(fwrite(&nbands, sizeof(nbands), 1, fp) == 1);
+ CHK(fwrite(&nnodes, sizeof(nnodes), 1, fp) == 1);
+ iband = 0;
+ low = 1;
+ upp = 0;
+ nqpts = 1;
+ CHK(fwrite(&iband, sizeof(iband), 1, fp) == 1);
+ CHK(fwrite(&low, sizeof(low), 1, fp) == 1);
+ CHK(fwrite(&upp, sizeof(upp), 1, fp) == 1);
+ CHK(fwrite(&nqpts, sizeof(nqpts), 1, fp) == 1);
+ abscissa = 1;
+ weight = 1;
+ CHK(fwrite(&abscissa, sizeof(abscissa), 1, fp) == 1);
+ CHK(fwrite(&weight, sizeof(weight), 1, fp) == 1);
+ CHK(fseek(fp, (long)ALIGN_SIZE((size_t)ftell(fp), pagesize), SEEK_SET) == 0);
+ ka = 1;
+ CHK(fwrite(&ka, sizeof(ka), 1, fp) == 1);
+ CHK(fseek(fp, (long)ALIGN_SIZE((size_t)ftell(fp), pagesize)-1, SEEK_SET) == 0);
+ CHK(fwrite(&byte, sizeof(byte), 1, fp) == 1); /* Positioned the EOF */
+ rewind(fp);
+
+
+ CHK(atrck_load_stream(atrck, fp, NULL) == RES_BAD_ARG);
+ fclose(fp);
+}
+
+static void
test_load_files(struct atrck* atrck, int argc, char** argv)
{
int i;
@@ -218,6 +337,7 @@ main(int argc, char** argv)
CHK(atrck_create(NULL, &mem_default_allocator, 1, &atrck) == RES_OK);
test_load(atrck);
+ test_load_fail(atrck);
test_load_files(atrck, argc, argv);
CHK(atrck_ref_put(atrck) == RES_OK);