commit f1445d85a8ade62fec54ddad1099b9ab5802cd37
parent 6ee2ce25363acc1c2f567d3b05788d625a28b989
Author: Christian Grothoff <christian@grothoff.org>
Date: Sat, 12 Jun 2010 22:57:40 +0000
null malloc checks
Diffstat:
12 files changed, 92 insertions(+), 63 deletions(-)
diff --git a/src/common/convert.c b/src/common/convert.c
@@ -53,7 +53,7 @@ EXTRACTOR_common_convert_to_utf8 (const char *input, size_t len, const char *cha
return NULL;
itmp = tmp;
finSize = tmpSize;
- if (iconv (cd, (char **) &input, &len, &itmp, &finSize) == (size_t) - 1)
+ if (iconv (cd, (char **) &input, &len, &itmp, &finSize) == SIZE_MAX)
{
iconv_close (cd);
free (tmp);
diff --git a/src/include/platform.h b/src/include/platform.h
@@ -44,6 +44,7 @@
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
+#include <stdint.h>
#include <fcntl.h>
#include <time.h>
#include <utime.h>
@@ -65,6 +66,10 @@
#include <iconv.h>
#include <langinfo.h>
+#ifndef SIZE_MAX
+#define SIZE_MAX ((size_t)-1)
+#endif
+
#if DARWIN
#include <mach-o/dyld.h>
#include <mach-o/ldsyms.h>
diff --git a/src/main/extractor.c b/src/main/extractor.c
@@ -1249,7 +1249,7 @@ process_requests (struct EXTRACTOR_PluginList *plugin,
#ifndef WINDOWS
if ( (-1 != (shmid = shm_open (fn, O_RDONLY, 0))) &&
- (((size_t)-1) != (size = lseek (shmid, 0, SEEK_END))) &&
+ (SIZE_MAX != (size = lseek (shmid, 0, SEEK_END))) &&
(NULL != (ptr = mmap (NULL, size, PROT_READ, MAP_SHARED, shmid, 0))) &&
(ptr != (void*) -1) )
#else
diff --git a/src/main/iconv.c b/src/main/iconv.c
@@ -24,7 +24,8 @@
*/
static char *
iconv_helper(iconv_t cd,
- const char * in) {
+ const char * in)
+{
size_t inSize;
char * buf;
char * ibuf;
@@ -50,10 +51,11 @@ iconv_helper(iconv_t cd,
(char**) &in,
&inSize,
&ibuf,
- &outLeft) == (size_t)-1) {
- /* conversion failed */
- free(buf);
- return strdup(i);
- }
+ &outLeft) == SIZE_MAX)
+ {
+ /* conversion failed */
+ free(buf);
+ return strdup(i);
+ }
return buf;
}
diff --git a/src/plugins/deb_extractor.c b/src/plugins/deb_extractor.c
@@ -223,9 +223,6 @@ processControlTar (const char *data,
}
#define MAX_CONTROL_SIZE (1024 * 1024)
-#ifndef SIZE_MAX
-#define SIZE_MAX ((size_t)-1)
-#endif
static voidpf
Emalloc (voidpf opaque, uInt items, uInt size)
diff --git a/src/plugins/dvi_extractor.c b/src/plugins/dvi_extractor.c
@@ -71,16 +71,23 @@ parseZZZ (const char *data,
slen++;
slen = slen - pos;
value = malloc (slen + 1);
- value[slen] = '\0';
- memcpy (value, &data[pos], slen);
- if (0 != proc (proc_cls,
- "dvi",
- tmap[i].type,
- EXTRACTOR_METAFORMAT_C_STRING,
- "text/plain",
- value,
- strlen (value) +1))
- return 1;
+ if (value != NULL)
+ {
+ value[slen] = '\0';
+ memcpy (value, &data[pos], slen);
+ if (0 != proc (proc_cls,
+ "dvi",
+ tmap[i].type,
+ EXTRACTOR_METAFORMAT_C_STRING,
+ "text/plain",
+ value,
+ strlen (value) +1))
+ {
+ free (value);
+ return 1;
+ }
+ free (value);
+ }
pos += slen + 1;
}
}
@@ -119,8 +126,8 @@ EXTRACTOR_dvi_extract (const unsigned char *data,
{
unsigned int klen;
char *comment;
- unsigned int pos;
- unsigned int opos;
+ uint32_t pos;
+ uint32_t opos;
unsigned int len;
unsigned int pageCount;
char pages[16];
@@ -153,7 +160,7 @@ EXTRACTOR_dvi_extract (const unsigned char *data,
pos = ntohl (getIntAt (&data[opos + 1]));
while (1)
{
- if (pos == (unsigned int) -1)
+ if (pos == UINT32_MAX)
break;
if (pos + 45 > size)
return 0;
@@ -162,7 +169,7 @@ EXTRACTOR_dvi_extract (const unsigned char *data,
pageCount++;
opos = pos;
pos = ntohl (getIntAt (&data[opos + 41]));
- if (pos == (unsigned int) -1)
+ if (pos == UINT32_MAX)
break;
if (pos >= opos)
return 0; /* invalid! */
@@ -171,35 +178,38 @@ EXTRACTOR_dvi_extract (const unsigned char *data,
snprintf (pages, sizeof(pages), "%u", pageCount);
if (0 != proc (proc_cls,
"dvi",
- EXTRACTOR_METATYPE_MIMETYPE,
+ EXTRACTOR_METATYPE_PAGE_COUNT,
EXTRACTOR_METAFORMAT_UTF8,
"text/plain",
- "application/x-dvi",
- strlen ("application/x-dvi") +1))
+ pages,
+ strlen (pages) +1))
return 1;
- comment = malloc (klen + 1);
- comment[klen] = '\0';
- memcpy (comment, &data[15], klen);
if (0 != proc (proc_cls,
"dvi",
- EXTRACTOR_METATYPE_COMMENT,
+ EXTRACTOR_METATYPE_MIMETYPE,
EXTRACTOR_METAFORMAT_UTF8,
"text/plain",
- comment,
- strlen (comment) +1))
+ "application/x-dvi",
+ strlen ("application/x-dvi") +1))
+ return 1;
+ comment = malloc (klen + 1);
+ if (comment != NULL)
{
+ comment[klen] = '\0';
+ memcpy (comment, &data[15], klen);
+ if (0 != proc (proc_cls,
+ "dvi",
+ EXTRACTOR_METATYPE_COMMENT,
+ EXTRACTOR_METAFORMAT_UTF8,
+ "text/plain",
+ comment,
+ strlen (comment) +1))
+ {
+ free (comment);
+ return 1;
+ }
free (comment);
- return 1;
}
- free (comment);
- if (0 != proc (proc_cls,
- "dvi",
- EXTRACTOR_METATYPE_PAGE_COUNT,
- EXTRACTOR_METAFORMAT_UTF8,
- "text/plain",
- pages,
- strlen (pages) +1))
- return 1;
/* try to find PDF/ps special */
pos = opos;
while (pos < size - 100)
diff --git a/src/plugins/flac_extractor.c b/src/plugins/flac_extractor.c
@@ -134,6 +134,8 @@ static char * xstrndup(const char * s, size_t n){
char * d;
d= malloc(n+1);
+ if (d == NULL)
+ return NULL;
memcpy(d,s,n);
d[n]='\0';
return d;
@@ -162,8 +164,11 @@ check(const char * type,
{
tmp = xstrndup(value,
value_length);
- ADD (tmap[i].type, tmp);
- free (tmp);
+ if (tmp != NULL)
+ {
+ ADD (tmap[i].type, tmp);
+ free (tmp);
+ }
break;
}
i++;
diff --git a/src/plugins/jpeg_extractor.c b/src/plugins/jpeg_extractor.c
@@ -112,6 +112,8 @@ process_COM (const unsigned char **data, const unsigned char *end)
if (length <= 0)
return NULL;
comment = malloc (length + 1);
+ if (comment == NULL)
+ return NULL;
pos = 0;
while (length > 0)
{
diff --git a/src/plugins/pdf_extractor.cc b/src/plugins/pdf_extractor.cc
@@ -70,25 +70,25 @@ printInfoString(Dict *infoDict,
} else {
size_t len = strlen(s);
- while(0 < len) {
- /*
- * Avoid outputting trailing spaces.
- *
- * The following expression might be rewritten as
- * (! isspace(s[len - 1]) && 0xA0 != s[len - 1]).
- * There seem to exist isspace() implementations
- * which do return non-zero from NBSP (maybe locale-dependent).
- * Remove ISO-8859 non-breaking space (NBSP, hex value 0xA0) from
- * the expression if it looks suspicious (locale issues for instance).
- *
- * Squeezing out all non-printable characters might also be useful.
- */
- if ( (' ' != s[len - 1]) && ((char)0xA0 != s[len - 1]) &&
+ while(0 < len)
+ {
+ /*
+ * Avoid outputting trailing spaces.
+ *
+ * The following expression might be rewritten as
+ * (! isspace(s[len - 1]) && 0xA0 != s[len - 1]).
+ * There seem to exist isspace() implementations
+ * which do return non-zero from NBSP (maybe locale-dependent).
+ * Remove ISO-8859 non-breaking space (NBSP, hex value 0xA0) from
+ * the expression if it looks suspicious (locale issues for instance).
+ *
+ * Squeezing out all non-printable characters might also be useful.
+ */
+ if ( (' ' != s[len - 1]) && (((char)0xA0) != s[len - 1]) &&
('\r' != s[len - 1]) && ('\n' != s[len - 1]) &&
('\t' != s[len - 1]) && ('\v' != s[len - 1]) &&
('\f' != s[len - 1]) )
- break;
-
+ break;
else
len --;
}
diff --git a/src/plugins/png_extractor.c b/src/plugins/png_extractor.c
@@ -28,6 +28,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;
@@ -79,7 +81,7 @@ static struct
#define ADD(t,s) do { if (0 != (ret = proc (proc_cls, "tar", t, EXTRACTOR_METAFORMAT_UTF8, "text/plain", s, strlen(s)+1))) goto FINISH; } while (0)
-#define ADDF(t,s) do { if (0 != (ret = proc (proc_cls, "tar", t, EXTRACTOR_METAFORMAT_UTF8, "text/plain", s, strlen(s)+1))) { free(s); goto FINISH; } free (s); } while (0)
+#define ADDF(t,s) do { if ( (s != NULL) && (0 != (ret = proc (proc_cls, "tar", t, EXTRACTOR_METAFORMAT_UTF8, "text/plain", s, strlen(s)+1))) ) { free(s); goto FINISH; } if (s != NULL) free (s); } while (0)
static int
diff --git a/src/plugins/ps_extractor.c b/src/plugins/ps_extractor.c
@@ -39,6 +39,8 @@ readline (const char *data, size_t size, size_t pos)
(data[end] != (char) 0x0d) && (data[end] != (char) 0x0a))
end++;
res = malloc (end - pos + 1);
+ if (res == NULL)
+ return NULL;
memcpy (res, &data[pos], end - pos);
res[end - pos] = '\0';
diff --git a/src/plugins/qt_extractor.c b/src/plugins/qt_extractor.c
@@ -853,6 +853,8 @@ processTextTag (const char *input,
addKeyword (EXTRACTOR_METATYPE_LANGUAGE, languages[lang], ec);
meta = malloc (len + 1);
+ if (meta == NULL)
+ return 0;
memcpy (meta, &txt[1], len);
meta[len] = '\0';
for (i = 0; i < len; i++)
@@ -951,6 +953,8 @@ processDataAtom (const char *input,
}
else if (flags == 0x1) { /* text data */
meta = malloc (len + 1);
+ if (meta == NULL)
+ return 0;
memcpy (meta, &input[pos+16], len);
meta[len] = '\0';
for (i = 0; i < len; i++)