summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorzwelch <zwelch@b42882b7-edfa-0310-969c-e2dbd0fdcd60>2009-05-08 05:30:37 +0000
committerzwelch <zwelch@b42882b7-edfa-0310-969c-e2dbd0fdcd60>2009-05-08 05:30:37 +0000
commit6d60d226874d2a8310550f41b3fef81139f582a1 (patch)
treec7fe61ca2122df6838d7b70eda8435ad243a246b
parent641919d491428cb4eb9d91354e49736b9de854a6 (diff)
downloadopenocd_libswd-6d60d226874d2a8310550f41b3fef81139f582a1.tar.gz
openocd_libswd-6d60d226874d2a8310550f41b3fef81139f582a1.tar.bz2
openocd_libswd-6d60d226874d2a8310550f41b3fef81139f582a1.tar.xz
openocd_libswd-6d60d226874d2a8310550f41b3fef81139f582a1.zip
Fix loadFile to return file length once again.
git-svn-id: svn://svn.berlios.de/openocd/trunk@1661 b42882b7-edfa-0310-969c-e2dbd0fdcd60
-rw-r--r--src/helper/ioutil.c14
1 files changed, 9 insertions, 5 deletions
diff --git a/src/helper/ioutil.c b/src/helper/ioutil.c
index 66908aff..51f58f0f 100644
--- a/src/helper/ioutil.c
+++ b/src/helper/ioutil.c
@@ -91,6 +91,9 @@ int handle_rm_command(struct command_context_s *cmd_ctx, char *cmd,
* 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 @@ int loadFile(const char *fileName, void **data, size_t *len)
fclose(pFile);
return ERROR_FAIL;
}
+ *len = fsize;
if (fseek(pFile, 0, SEEK_SET)!=0)
{
@@ -118,7 +122,7 @@ int loadFile(const char *fileName, void **data, size_t *len)
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 @@ int loadFile(const char *fileName, void **data, size_t *len)
return ERROR_FAIL;
}
fclose(pFile);
- *(((char *)(*data))+*len)=0; /* sentinel */
-
- return ERROR_OK;
-
+ // 0-byte after buffer (not included in *len) serves as a sentinel
+ char *buf = (char *)*data;
+ buf[*len = 0;
+ return ERROR_OK;
}