|
From: <sv...@va...> - 2014-07-29 19:44:27
|
Author: florian
Date: Tue Jul 29 19:44:16 2014
New Revision: 14203
Log:
Avoid fixed size buffer in VG_(basename) and VG_(dirname). Instead
dynamically allocate it.
An alternative would have been for the caller to provide a large enough
buffer which can always be done because the length of the basename and
dirname is always smaller than the length of the path we're starting
out with. But it leads to some clumsyness on the call side:
len = strlen(path) + 1;
HChar buf[len];
VG_(basename_(path, buf, len);
And we'd still had to check that we don't write more than len characters
into buf.
Modified:
branches/BUF_REMOVAL/coregrind/m_libcfile.c
Modified: branches/BUF_REMOVAL/coregrind/m_libcfile.c
==============================================================================
--- branches/BUF_REMOVAL/coregrind/m_libcfile.c (original)
+++ branches/BUF_REMOVAL/coregrind/m_libcfile.c Tue Jul 29 19:44:16 2014
@@ -37,8 +37,8 @@
#include "pub_core_libcfile.h"
#include "pub_core_libcprint.h" // VG_(sprintf)
#include "pub_core_libcproc.h" // VG_(getpid), VG_(getppid)
-#include "pub_core_xarray.h"
#include "pub_core_clientstate.h" // VG_(fd_hard_limit)
+#include "pub_core_mallocfree.h" // VG_(arena_realloc)
#include "pub_core_syscall.h"
/* IMPORTANT: on Darwin it is essential to use the _nocancel versions
@@ -1165,11 +1165,10 @@
# endif
}
-
const HChar *VG_(basename)(const HChar *path)
{
- static HChar buf[VKI_PATH_MAX];
-
+ static HChar *buf = NULL;
+ static SizeT buf_len = 0;
const HChar *p, *end;
if (path == NULL ||
@@ -1195,6 +1194,11 @@
if (*p == '/') p++;
+ SizeT need = end-p+1 + 1;
+ if (need > buf_len) {
+ buf_len = (buf_len == 0) ? VKI_PATH_MAX : need;
+ buf = VG_(arena_realloc)(VG_AR_CORE, "basename", buf, buf_len);
+ }
VG_(strncpy)(buf, p, end-p+1);
buf[end-p+1] = '\0';
@@ -1204,7 +1208,8 @@
const HChar *VG_(dirname)(const HChar *path)
{
- static HChar buf[VKI_PATH_MAX];
+ static HChar *buf = NULL;
+ static SizeT buf_len = 0;
const HChar *p;
@@ -1236,6 +1241,11 @@
p--;
}
+ SizeT need = p-path+1 + 1;
+ if (need > buf_len) {
+ buf_len = (buf_len == 0) ? VKI_PATH_MAX : need;
+ buf = VG_(arena_realloc)(VG_AR_CORE, "dirname", buf, buf_len);
+ }
VG_(strncpy)(buf, path, p-path+1);
buf[p-path+1] = '\0';
|