|
From: <zw...@ma...> - 2009-05-08 07:30:52
|
Author: zwelch
Date: 2009-05-08 07:30:37 +0200 (Fri, 08 May 2009)
New Revision: 1661
Modified:
trunk/src/helper/ioutil.c
Log:
Fix loadFile to return file length once again.
Modified: trunk/src/helper/ioutil.c
===================================================================
--- trunk/src/helper/ioutil.c 2009-05-08 04:59:05 UTC (rev 1660)
+++ trunk/src/helper/ioutil.c 2009-05-08 05:30:37 UTC (rev 1661)
@@ -91,6 +91,9 @@
* a 0 byte(sentinel) after len bytes - the length of the file. */
int loadFile(const char *fileName, void **data, size_t *len)
{
+ // ensure returned length is always sane
+ *len = 0;
+
FILE * pFile;
pFile = fopen(fileName,"rb");
if (pFile==NULL)
@@ -111,6 +114,7 @@
fclose(pFile);
return ERROR_FAIL;
}
+ *len = fsize;
if (fseek(pFile, 0, SEEK_SET)!=0)
{
@@ -118,7 +122,7 @@
fclose(pFile);
return ERROR_FAIL;
}
- *data = malloc(fsize + 1);
+ *data = malloc(*len + 1);
if (*data==NULL)
{
LOG_ERROR("Can't open %s\n", fileName);
@@ -134,12 +138,12 @@
return ERROR_FAIL;
}
fclose(pFile);
- *(((char *)(*data))+*len)=0; /* sentinel */
+ // 0-byte after buffer (not included in *len) serves as a sentinel
+ char *buf = (char *)*data;
+ buf[*len = 0;
+
return ERROR_OK;
-
-
-
}
|