From d2a8aa0d2dddecf7cfbf67c67e6947256b340a00 Mon Sep 17 00:00:00 2001 From: Rob Latham Date: Thu, 3 Apr 2014 13:49:16 -0500 Subject: [PATCH] assorted memory leak cleanups tidy up a few stray memory allocations. Note: these changes make HumanReadable no longer re-entrant. --- src/ior.c | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/src/ior.c b/src/ior.c index d4533d7..c0b8d5b 100755 --- a/src/ior.c +++ b/src/ior.c @@ -991,6 +991,7 @@ static void GetTestFileName(char *testFileName, IOR_param_t * test) sprintf(tmpString, ".%d", test->repCounter); strcat(testFileName, tmpString); } + free (fileNames); } /* @@ -1019,18 +1020,15 @@ static double GetTimeStamp(void) } /* - * Convert IOR_offset_t value to human readable string. + * Convert IOR_offset_t value to human readable string. This routine uses a + * statically-allocated buffer internally and so is not re-entrant. */ static char *HumanReadable(IOR_offset_t value, int base) { - char *valueStr; + static char valueStr[MAX_STR]; int m = 0, g = 0; char m_str[8], g_str[8]; - valueStr = (char *)malloc(MAX_STR); - if (valueStr == NULL) - ERR("out of memory"); - if (base == BASE_TWO) { m = MEBIBYTE; g = GIBIBYTE; @@ -1045,22 +1043,22 @@ static char *HumanReadable(IOR_offset_t value, int base) if (value >= g) { if (value % (IOR_offset_t) g) { - sprintf(valueStr, "%.2f %s", + snprintf(valueStr, MAX_STR-1, "%.2f %s", (double)((double)value / g), g_str); } else { - sprintf(valueStr, "%d %s", (int)(value / g), g_str); + snprintf(valueStr, MAX_STR-1, "%d %s", (int)(value / g), g_str); } } else if (value >= m) { if (value % (IOR_offset_t) m) { - sprintf(valueStr, "%.2f %s", + snprintf(valueStr, MAX_STR-1, "%.2f %s", (double)((double)value / m), m_str); } else { - sprintf(valueStr, "%d %s", (int)(value / m), m_str); + snprintf(valueStr, MAX_STR-1, "%d %s", (int)(value / m), m_str); } } else if (value >= 0) { - sprintf(valueStr, "%d bytes", (int)value); + snprintf(valueStr, MAX_STR-1, "%d bytes", (int)value); } else { - sprintf(valueStr, "-"); + snprintf(valueStr, MAX_STR-1, "-"); } return valueStr; }