This is an automated email from the git hooks/post-receive script. It was generated because a ref change was pushed to the repository containing the project gfxprim.git.
The branch, master has been updated via f7abc056071b6987bd8ddf3b3e21b76fb2faa593 (commit) via f8f4f70933e49090c3910415315730aaf23cfa71 (commit) via e09dcfd1600590e8bc57039293fbf39eba0d6fd2 (commit) from 5e9ae922f961ce558878ad261b34db3dd7a99b6d (commit)
Those revisions listed above that are new to this repository have not appeared on any other notification email; so we list those revisions in full, below.
- Log ----------------------------------------------------------------- http://repo.or.cz/w/gfxprim.git/commit/f7abc056071b6987bd8ddf3b3e21b76fb2faa...
commit f7abc056071b6987bd8ddf3b3e21b76fb2faa593 Author: Cyril Hrubis metan@ucw.cz Date: Fri Sep 14 17:20:48 2012 +0200
loaders: Small fixes in errno handling code.
diff --git a/libs/loaders/GP_PBM.c b/libs/loaders/GP_PBM.c index ac7dc30..2c11138 100644 --- a/libs/loaders/GP_PBM.c +++ b/libs/loaders/GP_PBM.c @@ -54,28 +54,34 @@ GP_Context *GP_LoadPBM(const char *src_path, GP_ProgressCallback *callback) FILE *f; GP_Context *ret; uint32_t w, h; + int err;
f = fopen(src_path, "r"); if (f == NULL) return NULL;
- if (fgetc(f) != 'P' || fgetc(f) != '1') + if (fgetc(f) != 'P' || fgetc(f) != '1') { + err = feof(f) ? EIO : EINVAL; goto err1; + }
- if (fscanf(f, "%"PRIu32"%"PRIu32, &w, &h) < 2) + if (fscanf(f, "%"PRIu32"%"PRIu32, &w, &h) < 2) { + err = EIO; goto err1; + }
ret = GP_ContextAlloc(w, h, GP_PIXEL_G1);
if (ret == NULL) { - fclose(f); - errno = ENOMEM; - return NULL; + err = ENOMEM; + goto err1; }
- if (GP_PXMLoad1bpp(f, ret)) + if (GP_PXMLoad1bpp(f, ret)) { + err = EINVAL; goto err2; + } fclose(f); return ret; @@ -83,6 +89,7 @@ err2: free(ret); err1: fclose(f); + errno = err; return NULL; }
diff --git a/libs/loaders/GP_PGM.c b/libs/loaders/GP_PGM.c index 9ae8c64..055df97 100644 --- a/libs/loaders/GP_PGM.c +++ b/libs/loaders/GP_PGM.c @@ -92,6 +92,11 @@ GP_Context *GP_LoadPGM(const char *src_path, GP_ProgressCallback *callback) h1 = fgetc(f); h2 = fgetc(f);
+ if (feof(f)) { + err = EIO; + goto err1; + } + if (h1 != 'P' || h2 != '2') { GP_DEBUG(1, "Invalid PGM header '%c%c' (0x%2x 0x%2x)", isprint(h1) ? h1 : ' ', isprint(h2) ? h2 : ' ', diff --git a/libs/loaders/GP_PNG.c b/libs/loaders/GP_PNG.c index 2d099fd..73b129a 100644 --- a/libs/loaders/GP_PNG.c +++ b/libs/loaders/GP_PNG.c @@ -58,7 +58,7 @@ int GP_OpenPNG(const char *src_path, FILE **f) }
if (fread(sig, 1, 8, *f) <= 0) { - err = errno; + err = EIO; GP_DEBUG(1, "Failed to read '%s' : %s", src_path, strerror(errno)); goto err2; @@ -67,7 +67,7 @@ int GP_OpenPNG(const char *src_path, FILE **f) if (png_sig_cmp(sig, 0, 8)) { GP_DEBUG(1, "Invalid file header, '%s' not a PNG image?", src_path); - err = EILSEQ; + err = EINVAL; goto err2; }
diff --git a/libs/loaders/GP_PNM.c b/libs/loaders/GP_PNM.c index 261b75f..6e7946e 100644 --- a/libs/loaders/GP_PNM.c +++ b/libs/loaders/GP_PNM.c @@ -88,7 +88,7 @@ FILE *GP_ReadPNM(const char *src_path, char *fmt, uint32_t *w, uint32_t *h, uint32_t *depth) { FILE *f = fopen(src_path, "r"); - int ch; + int ch, err;
if (f == NULL) { GP_DEBUG(1, "Failed to open file '%s': %s", @@ -98,9 +98,15 @@ FILE *GP_ReadPNM(const char *src_path, char *fmt,
ch = fgetc(f);
+ if (ch == EOF) { + err = EIO; + goto err1; + } + if (ch != 'P') { GP_DEBUG(1, "Invalid PNM header start '%c' (0x%2x) expecting 'P'", isprint(ch) ? ch : ' ', ch); + err = EINVAL; goto err1; }
@@ -119,6 +125,7 @@ FILE *GP_ReadPNM(const char *src_path, char *fmt, default: GP_DEBUG(1, "Invalid PNM format 'P%c' (0x%2x)", isprint(ch) ? ch : ' ', ch); + err = EINVAL; goto err1; } @@ -128,6 +135,7 @@ FILE *GP_ReadPNM(const char *src_path, char *fmt,
if (fscanf(f, "%"PRIu32"n", w) < 1) { GP_DEBUG(1, "Failed to read PNM header width"); + err = EINVAL; goto err1; } @@ -135,6 +143,7 @@ FILE *GP_ReadPNM(const char *src_path, char *fmt, if (fscanf(f, "%"PRIu32"n", h) < 1) { GP_DEBUG(1, "Failed to read PNM header height"); + err = EINVAL; goto err1; }
@@ -148,12 +157,14 @@ FILE *GP_ReadPNM(const char *src_path, char *fmt, if (fscanf(f, "%"PRIu32"n", depth) < 1) { GP_DEBUG(1, "Failed to read PNM header depth"); + err = EINVAL; goto err1; }
return f; err1: fclose(f); + errno = err; return NULL; }
http://repo.or.cz/w/gfxprim.git/commit/f8f4f70933e49090c3910415315730aaf23cf...
commit f8f4f70933e49090c3910415315730aaf23cfa71 Author: Cyril Hrubis metan@ucw.cz Date: Fri Sep 14 17:15:54 2012 +0200
tests: Add common image loader tests.
diff --git a/tests/loaders/loaders_suite.c b/tests/loaders/loaders_suite.c index cb7f398..918b139 100644 --- a/tests/loaders/loaders_suite.c +++ b/tests/loaders/loaders_suite.c @@ -221,6 +221,91 @@ static int test_PNG_stress(void) return TST_SUCCESS; }
+/* + * Loaders test. Hammers the GP_LoadImage() interface with plenty of + * unexpected filenames. + */ +struct file_testcase { + const char *filename; + int create; + int expected_errno; +}; + +static struct file_testcase file_testcases[] = { + {"a", 1, ENOSYS}, + {".a", 1, ENOSYS}, + {"a.", 1, ENOSYS}, + {".bc", 1, ENOSYS}, + {"bc", 1, ENOSYS}, + {"abc", 1, ENOSYS}, + {"png.", 1, ENOSYS}, + {"jpg.", 1, ENOSYS}, + {"gif.", 1, ENOSYS}, + {"jpeg.", 1, ENOSYS}, + + {".jpg", 1, EIO}, + {"img.jpg", 1, EIO}, + {".png", 1, EIO}, + {"img.png", 1, EIO}, + {".gif", 1, EIO}, + {"img.gif", 1, EIO}, + {".pbm", 1, EIO}, + {".pgm", 1, EIO}, + {".ppm", 1, EIO}, + + {"not_here.jpg", 0, ENOENT}, + + {NULL, 0, 0} +}; + +static int test_Load(void) +{ + unsigned int i, fail = 0; + + /* Create empty files */ + for (i = 0; file_testcases[i].filename != NULL; i++) { + + if (file_testcases[i].create != 1) + continue; + + FILE *f = fopen(file_testcases[i].filename, "w"); + + if (f != NULL) + fclose(f); + } + + for (i = 0; file_testcases[i].filename != NULL; i++) { + GP_Context *ret; + + errno = 0; + + ret = GP_LoadImage(file_testcases[i].filename, NULL); + + int saved_errno = errno; + + if (ret != NULL) { + tst_report(0, "GP_LoadImage('%s') succeeded " + "unexpectedly", file_testcases[i].filename); + fail++; + } + + if (ret == NULL && file_testcases[i].expected_errno != errno) { + tst_report(0, "Expected errno %i (%s) got %i (%s) on '%s'", + file_testcases[i].expected_errno, + strerror(file_testcases[i].expected_errno), + saved_errno, + strerror(saved_errno), + file_testcases[i].filename); + fail++; + } + } + + if (fail) + return TST_FAILED; + + return TST_SUCCESS; +} + const struct tst_suite tst_suite = { .suite_name = "Image Loaders testsuite", .tests = { @@ -238,6 +323,8 @@ const struct tst_suite tst_suite = { .flags = TST_TMPDIR | TST_CHECK_MALLOC}, {.name = "PNG stress", .tst_fn = test_PNG_stress, .flags = TST_TMPDIR | TST_CHECK_MALLOC}, + {.name = "Image Load", .tst_fn = test_Load, + .flags = TST_TMPDIR}, {.name = NULL}, } };
http://repo.or.cz/w/gfxprim.git/commit/e09dcfd1600590e8bc57039293fbf39eba0d6...
commit e09dcfd1600590e8bc57039293fbf39eba0d6fd2 Author: Cyril Hrubis metan@ucw.cz Date: Fri Sep 14 16:42:54 2012 +0200
loaders: Simplify the image format detection code.
diff --git a/libs/loaders/GP_Loaders.c b/libs/loaders/GP_Loaders.c index 5700aa4..4e3c3d6 100644 --- a/libs/loaders/GP_Loaders.c +++ b/libs/loaders/GP_Loaders.c @@ -34,108 +34,141 @@
#include "GP_Loaders.h"
-GP_Context *GP_LoadImage(const char *src_path, GP_ProgressCallback *callback) +enum GP_ImageFmt { + GP_FMT_UNKNOWN, + GP_FMT_PNG, + GP_FMT_JPG, + GP_FMT_BMP, + GP_FMT_GIF, + GP_FMT_PBM, + GP_FMT_PGM, + GP_FMT_PPM, +}; + +enum GP_ImageFmt filename_to_fmt(const char *path) { - int len, saved_errno; - - if (access(src_path, R_OK)) { - - saved_errno = errno; - - GP_DEBUG(1, "Failed to access file '%s' : %s", - src_path, strerror(errno)); - - errno = saved_errno; - - return NULL; - } - - len = strlen(src_path); + size_t len = strlen(path);
if (len < 3) - goto skip_filename_check; + return GP_FMT_UNKNOWN;
- switch (src_path[len - 1]) { + switch (path[len - 1]) { /* PNG, JPG, JPEG */ case 'g': case 'G': - switch (src_path[len - 2]) { + switch (path[len - 2]) { case 'n': case 'N': - if (src_path[len - 3] == 'p' || - src_path[len - 3] == 'P') - return GP_LoadPNG(src_path, callback); + if (path[len - 3] == 'p' || + path[len - 3] == 'P') + return GP_FMT_PNG; break; case 'p': case 'P': - if (src_path[len - 3] == 'j' || - src_path[len - 3] == 'J') - return GP_LoadJPG(src_path, callback); + if (path[len - 3] == 'j' || + path[len - 3] == 'J') + return GP_FMT_JPG; break; case 'e': case 'E': - if ((src_path[len - 3] == 'p' || - src_path[len - 3] == 'P') && - (src_path[len - 4] == 'j' || - src_path[len - 4] == 'J')) - return GP_LoadJPG(src_path, callback); + if ((path[len - 3] == 'p' || + path[len - 3] == 'P') && + (path[len - 4] == 'j' || + path[len - 4] == 'J')) + return GP_FMT_JPG; break; } break; /* PPM, PGM, PBM, PNM */ case 'm': case 'M': - switch (src_path[len - 2]) { + switch (path[len - 2]) { case 'b': case 'B': - if (src_path[len - 3] == 'p' || - src_path[len - 3] == 'P') { - return GP_LoadPBM(src_path, callback); - } + if (path[len - 3] == 'p' || + path[len - 3] == 'P') + return GP_FMT_PBM; break; case 'g': case 'G': - if (src_path[len - 3] == 'p' || - src_path[len - 3] == 'P') { - return GP_LoadPGM(src_path, callback); - } + if (path[len - 3] == 'p' || + path[len - 3] == 'P') + return GP_FMT_PGM; break; case 'p': case 'P': - if (src_path[len - 3] == 'p' || - src_path[len - 3] == 'P') { - return GP_LoadPPM(src_path, callback); - } + if (path[len - 3] == 'p' || + path[len - 3] == 'P') + return GP_FMT_PPM; break; } break; /* BMP */ case 'P': case 'p': - switch (src_path[len - 2]) { + switch (path[len - 2]) { case 'M': case 'm': - if (src_path[len - 3] == 'B' || - src_path[len - 3] == 'b') - return GP_LoadBMP(src_path, callback); + if (path[len - 3] == 'B' || + path[len - 3] == 'b') + return GP_FMT_BMP; break; } break; /* GIF */ case 'F': case 'f': - switch (src_path[len - 2]) { + switch (path[len - 2]) { case 'I': case 'i': - if (src_path[len - 3] == 'G' || - src_path[len - 3] == 'g') - return GP_LoadGIF(src_path, callback); + if (path[len - 3] == 'G' || + path[len - 3] == 'g') + return GP_FMT_GIF; break; } break; }
-skip_filename_check: + return GP_FMT_UNKNOWN; +} + +GP_Context *GP_LoadImage(const char *src_path, GP_ProgressCallback *callback) +{ + int saved_errno; + + if (access(src_path, R_OK)) { + + saved_errno = errno; + + GP_DEBUG(1, "Failed to access file '%s' : %s", + src_path, strerror(errno)); + + errno = saved_errno; + + return NULL; + } + + enum GP_ImageFmt fmt = filename_to_fmt(src_path); + + switch (fmt) { + case GP_FMT_JPG: + return GP_LoadJPG(src_path, callback); + case GP_FMT_PNG: + return GP_LoadPNG(src_path, callback); + case GP_FMT_GIF: + return GP_LoadGIF(src_path, callback); + case GP_FMT_BMP: + return GP_LoadGIF(src_path, callback); + case GP_FMT_PBM: + return GP_LoadPBM(src_path, callback); + case GP_FMT_PGM: + return GP_LoadPGM(src_path, callback); + case GP_FMT_PPM: + return GP_LoadPPM(src_path, callback); + + case GP_FMT_UNKNOWN: + break; + }
//TODO file signature based check errno = ENOSYS; @@ -144,41 +177,29 @@ skip_filename_check:
int GP_LoadMetaData(const char *src_path, GP_MetaData *data) { - int len; + int saved_errno; - len = strlen(src_path); + if (access(src_path, R_OK)) { + + saved_errno = errno;
- if (len < 3) { - errno = ENOSYS; + GP_DEBUG(1, "Failed to access file '%s' : %s", + src_path, strerror(errno)); + + errno = saved_errno; + return 1; }
- switch (src_path[len - 1]) { - /* PNG, JPG, JPEG */ - case 'g': - case 'G': - switch (src_path[len - 2]) { - case 'n': - case 'N': - if (src_path[len - 3] == 'p' || - src_path[len - 3] == 'P') - return GP_LoadPNGMetaData(src_path, data); - break; - case 'p': - case 'P': - if (src_path[len - 3] == 'j' || - src_path[len - 3] == 'J') - return GP_LoadJPGMetaData(src_path, data); - break; - case 'e': - case 'E': - if ((src_path[len - 3] == 'p' || - src_path[len - 3] == 'P') && - (src_path[len - 4] == 'j' || - src_path[len - 4] == 'J')) - return GP_LoadJPGMetaData(src_path, data); - break; - } + enum GP_ImageFmt fmt = filename_to_fmt(src_path); + + switch (fmt) { + case GP_FMT_JPG: + return GP_LoadJPGMetaData(src_path, data); + case GP_FMT_PNG: + return GP_LoadPNGMetaData(src_path, data); + + default: break; }
@@ -189,41 +210,15 @@ int GP_LoadMetaData(const char *src_path, GP_MetaData *data) int GP_SaveImage(const GP_Context *src, const char *dst_path, GP_ProgressCallback *callback) { - int len; - - len = strlen(dst_path); + enum GP_ImageFmt fmt = filename_to_fmt(dst_path);
- if (len < 3) { - errno = ENOSYS; - return 1; - } + switch (fmt) { + case GP_FMT_JPG: + return GP_SaveJPG(src, dst_path, callback); + case GP_FMT_PNG: + return GP_SavePNG(src, dst_path, callback);
- switch (dst_path[len - 1]) { - /* PNG, JPG, JPEG */ - case 'g': - case 'G': - switch (dst_path[len - 2]) { - case 'n': - case 'N': - if (dst_path[len - 3] == 'p' || - dst_path[len - 3] == 'P') - return GP_SavePNG(src, dst_path, callback); - break; - case 'p': - case 'P': - if (dst_path[len - 3] == 'j' || - dst_path[len - 3] == 'J') - return GP_SaveJPG(src, dst_path, callback); - break; - case 'e': - case 'E': - if ((dst_path[len - 3] == 'p' || - dst_path[len - 3] == 'P') && - (dst_path[len - 4] == 'j' || - dst_path[len - 4] == 'J')) - return GP_SaveJPG(src, dst_path, callback); - break; - } + default: break; }
-----------------------------------------------------------------------
Summary of changes: libs/loaders/GP_Loaders.c | 225 ++++++++++++++++++++--------------------- libs/loaders/GP_PBM.c | 19 +++- libs/loaders/GP_PGM.c | 5 + libs/loaders/GP_PNG.c | 4 +- libs/loaders/GP_PNM.c | 13 ++- tests/loaders/loaders_suite.c | 87 ++++++++++++++++ 6 files changed, 229 insertions(+), 124 deletions(-)
repo.or.cz automatic notification. Contact project admin jiri.bluebear.dluhos@gmail.com if you want to unsubscribe, or site admin admin@repo.or.cz if you receive no reply.