libextractor

GNU libextractor
Log | Files | Refs | Submodules | README | LICENSE

commit 5e9a1a7e08cb7e116eddcd30f9bc528e0dc9ee96
parent 0b10d145926240dd86c1d70ff7f91258347a0d1c
Author: Christian Grothoff <christian@grothoff.org>
Date:   Sat, 12 Jun 2010 23:28:36 +0000

null checks and a few missing frees

Diffstat:
Msrc/plugins/deb_extractor.c | 9+++++++++
Msrc/plugins/html_extractor.c | 57+++++++++++++++++++++++++++++++++------------------------
Msrc/plugins/id3_extractor.c | 32++++++++++++++------------------
Msrc/plugins/man_extractor.c | 4++++
Msrc/plugins/nsfe_extractor.c | 46++++++++++++++++++++++++++++++----------------
Msrc/plugins/tiff_extractor.c | 11+++++++++--
6 files changed, 99 insertions(+), 60 deletions(-)

diff --git a/src/plugins/deb_extractor.c b/src/plugins/deb_extractor.c @@ -40,6 +40,8 @@ stndup (const char *str, size_t n) { char *tmp; tmp = malloc (n + 1); + if (tmp == NULL) + return NULL; tmp[n] = '\0'; memcpy (tmp, str, n); return tmp; @@ -114,12 +116,19 @@ processControl (const char *data, if ((eol == colon) || (eol > size)) return 0; key = stndup (&data[pos], colon - pos); + if (key == NULL) + return 0; i = 0; while (tmap[i].text != NULL) { if (0 == strcmp (key, tmap[i].text)) { val = stndup (&data[colon], eol - colon); + if (val == NULL) + { + free (key); + return 0; + } if (0 != proc (proc_cls, "deb", tmap[i].type, diff --git a/src/plugins/html_extractor.c b/src/plugins/html_extractor.c @@ -339,26 +339,31 @@ EXTRACTOR_html_extract (const char *data, (ret == 0) ) { if (charset == NULL) - ret = proc (proc_cls, - "html", - tagmap[i].type, - EXTRACTOR_METAFORMAT_C_STRING, - "text/plain", - tmp, - strlen (tmp) + 1); - else { - xtmp = EXTRACTOR_common_convert_to_utf8 (tmp, - strlen (tmp), - charset); ret = proc (proc_cls, "html", tagmap[i].type, - EXTRACTOR_METAFORMAT_UTF8, + EXTRACTOR_METAFORMAT_C_STRING, "text/plain", - xtmp, - strlen (xtmp) + 1); - free (xtmp); + tmp, + strlen (tmp) + 1); + } + else + { + xtmp = EXTRACTOR_common_convert_to_utf8 (tmp, + strlen (tmp), + charset); + if (xtmp != NULL) + { + ret = proc (proc_cls, + "html", + tagmap[i].type, + EXTRACTOR_METAFORMAT_UTF8, + "text/plain", + xtmp, + strlen (xtmp) + 1); + free (xtmp); + } } } if (tmp != NULL) @@ -393,19 +398,23 @@ EXTRACTOR_html_extract (const char *data, xtmp = EXTRACTOR_common_convert_to_utf8 (t->dataStart, t->dataEnd - t->dataStart, charset); - ret = proc (proc_cls, - "html", - EXTRACTOR_METATYPE_TITLE, - EXTRACTOR_METAFORMAT_UTF8, - "text/plain", - xtmp, - strlen (xtmp) + 1); - free (xtmp); + if (xtmp != NULL) + { + ret = proc (proc_cls, + "html", + EXTRACTOR_METATYPE_TITLE, + EXTRACTOR_METAFORMAT_UTF8, + "text/plain", + xtmp, + strlen (xtmp) + 1); + free (xtmp); + } } } tags = t->next; free (t); } - free (charset); + if (charset != NULL) + free (charset); return ret; } diff --git a/src/plugins/id3_extractor.c b/src/plugins/id3_extractor.c @@ -202,6 +202,8 @@ static const char *const genre_names[] = { static void trim (char *k) { + if (k == NULL) + return; while ((strlen (k) > 0) && (isspace ((unsigned char) k[strlen (k) - 1]))) k[strlen (k) - 1] = '\0'; } @@ -251,7 +253,7 @@ get_id3 (const char *data, size_t size, id3tag * id3) } -#define ADD(s,t) do { if (0 != (ret = proc (proc_cls, "id3", t, EXTRACTOR_METAFORMAT_UTF8, "text/plain", s, strlen(s)+1))) goto FINISH; } while (0) +#define ADD(s,t) do { if ( (s != NULL) && (strlen(s) > 0) && (0 != (ret = proc (proc_cls, "id3", t, EXTRACTOR_METAFORMAT_UTF8, "text/plain", s, strlen(s)+1)))) goto FINISH; } while (0) const char * @@ -275,18 +277,12 @@ EXTRACTOR_id3_extract (const char *data, ret = 0; if (OK != get_id3 (data, size, &info)) return 0; - if (strlen (info.title) > 0) - ADD (info.title, EXTRACTOR_METATYPE_TITLE); - if (strlen (info.artist) > 0) - ADD (info.artist, EXTRACTOR_METATYPE_ARTIST); - if (strlen (info.album) > 0) - ADD (info.album, EXTRACTOR_METATYPE_ALBUM); - if (strlen (info.year) > 0) - ADD (info.year, EXTRACTOR_METATYPE_PUBLICATION_YEAR); - if (strlen (info.genre) > 0) - ADD (info.genre, EXTRACTOR_METATYPE_GENRE); - if (strlen (info.comment) > 0) - ADD (info.comment, EXTRACTOR_METATYPE_COMMENT); + ADD (info.title, EXTRACTOR_METATYPE_TITLE); + ADD (info.artist, EXTRACTOR_METATYPE_ARTIST); + ADD (info.album, EXTRACTOR_METATYPE_ALBUM); + ADD (info.year, EXTRACTOR_METATYPE_PUBLICATION_YEAR); + ADD (info.genre, EXTRACTOR_METATYPE_GENRE); + ADD (info.comment, EXTRACTOR_METATYPE_COMMENT); if (info.track_number != 0) { snprintf(track, @@ -294,11 +290,11 @@ EXTRACTOR_id3_extract (const char *data, ADD (track, EXTRACTOR_METATYPE_TRACK_NUMBER); } FINISH: - free (info.title); - free (info.year); - free (info.album); - free (info.artist); - free (info.comment); + if (info.title != NULL) free (info.title); + if (info.year != NULL) free (info.year); + if (info.album != NULL) free (info.album); + if (info.artist != NULL) free (info.artist); + if (info.comment != NULL) free (info.comment); return ret; } diff --git a/src/plugins/man_extractor.c b/src/plugins/man_extractor.c @@ -27,6 +27,8 @@ stndup (const char *str, size_t n) { char *tmp; tmp = malloc (n + 1); + if (tmp == NULL) + return NULL; tmp[n] = '\0'; memcpy (tmp, str, n); return tmp; @@ -53,6 +55,8 @@ addKeyword (enum EXTRACTOR_MetaType type, keyword[strlen (keyword) - 1] = '\0'; tmp = strdup (&keyword[1]); free (keyword); + if (tmp == NULL) + return 0; keyword = tmp; } if (strlen (keyword) == 0) diff --git a/src/plugins/nsfe_extractor.c b/src/plugins/nsfe_extractor.c @@ -84,6 +84,8 @@ nsfestring (const char *data, size_t size) (data[length] != '\0') ) length++; s = malloc (length + 1); + if (s == NULL) + return NULL; strncpy (s, data, length); s[strlen (data)] = '\0'; return s; @@ -157,8 +159,11 @@ libextractor_nsfe_tlbl_extract(const char *data, for (left = size; left > 0; left -= length) { title = nsfestring (&data[size - left], left); - length = strlen (title) + 1; - ADDF (title, EXTRACTOR_METATYPE_TITLE); + if (title != NULL) + { + length = strlen (title) + 1; + ADDF (title, EXTRACTOR_METATYPE_TITLE); + } } return 0; } @@ -177,25 +182,34 @@ libextractor_nsfe_auth_extract (const char *data, size_t size, if (left < 1) return 0; album = nsfestring (&data[size - left], left); - left -= (strlen (album) + 1); - ADDF (album, EXTRACTOR_METATYPE_ALBUM); - if (left < 1) - return 0; + if (album != NULL) + { + left -= (strlen (album) + 1); + ADDF (album, EXTRACTOR_METATYPE_ALBUM); + if (left < 1) + return 0; + } artist = nsfestring (&data[size - left], left); - left -= (strlen (artist) + 1); - ADDF (artist, EXTRACTOR_METATYPE_ARTIST); - if (left < 1) - return 0; + if (artist != NULL) + { + left -= (strlen (artist) + 1); + ADDF (artist, EXTRACTOR_METATYPE_ARTIST); + if (left < 1) + return 0; + } copyright = nsfestring (&data[size - left], left); - left -= (strlen (copyright) + 1); - ADDF (copyright, EXTRACTOR_METATYPE_COPYRIGHT); - if (left < 1) - return 0; - + if (copyright != NULL) + { + left -= (strlen (copyright) + 1); + ADDF (copyright, EXTRACTOR_METATYPE_COPYRIGHT); + if (left < 1) + return 0; + } ripper = nsfestring (&data[size - left], left); - ADDF (ripper, EXTRACTOR_METATYPE_RIPPER); + if (ripper != NULL) + ADDF (ripper, EXTRACTOR_METATYPE_RIPPER); return 0; } diff --git a/src/plugins/tiff_extractor.c b/src/plugins/tiff_extractor.c @@ -30,6 +30,8 @@ addKeyword (EXTRACTOR_MetaDataProcessor proc, const char *keyword, enum EXTRACTOR_MetaType type) { + if (keyword == NULL) + return 0; return proc (proc_cls, "tiff", type, @@ -188,7 +190,10 @@ EXTRACTOR_tiff_extract (const char *data, snprintf (tmp, sizeof(tmp), "%ux%u", width, length); - addKeyword (proc, proc_cls, strdup (tmp), EXTRACTOR_METATYPE_IMAGE_DIMENSIONS); + addKeyword (proc, + proc_cls, + tmp, + EXTRACTOR_METATYPE_IMAGE_DIMENSIONS); } break; case TAG_WIDTH: @@ -203,7 +208,9 @@ EXTRACTOR_tiff_extract (const char *data, sizeof(tmp), "%ux%u", width, length); - addKeyword (proc, proc_cls, strdup (tmp), EXTRACTOR_METATYPE_IMAGE_DIMENSIONS); + addKeyword (proc, proc_cls, + tmp, + EXTRACTOR_METATYPE_IMAGE_DIMENSIONS); } break; case TAG_SOFTWARE: