summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPetr Machata <pmachata@redhat.com>2009-09-02 20:46:08 +0200
committerPetr Machata <pmachata@redhat.com>2009-09-02 20:46:08 +0200
commitd3a8b2d3c636a024e17ff29b0f75d0a3cee8144e (patch)
treeeabb2d01f0505fc8cb88e7d5abe08210e0620802
parent05004af0dcab6efd0bb5a580f9534a3bff7537da (diff)
downloadelfutils-d3a8b2d3c636a024e17ff29b0f75d0a3cee8144e.tar.gz
libebl: Fix allocation of memory for long strings
-rw-r--r--libebl/ChangeLog5
-rw-r--r--libebl/eblstrtab.c19
2 files changed, 17 insertions, 7 deletions
diff --git a/libebl/ChangeLog b/libebl/ChangeLog
index 8c2f7df1..ba3dc7db 100644
--- a/libebl/ChangeLog
+++ b/libebl/ChangeLog
@@ -1,3 +1,8 @@
+2009-09-02 Petr Machata <pmachata@redhat.com>
+
+ * libebl/eblstrtab.c (morememory): Allocate memory in multiples of
+ pagesize.
+
2009-08-06 Petr Machata <pmachata@redhat.com>
* libebl/eblstrtab.c (ebl_strtabfinalize): Only call copystrings
diff --git a/libebl/eblstrtab.c b/libebl/eblstrtab.c
index ae4e6af0..4222cfd5 100644
--- a/libebl/eblstrtab.c
+++ b/libebl/eblstrtab.c
@@ -101,9 +101,11 @@ struct Ebl_Strtab
};
-/* Cache for the pagesize. We correct this value a bit so that `malloc'
- is not allocating more than a page. */
+/* Cache for the pagesize. */
static size_t ps;
+/* We correct this value a bit so that `malloc' is not allocating more
+ than a page. */
+#define MALLOC_OVERHEAD (2 * sizeof (void *))
struct Ebl_Strtab *
@@ -111,8 +113,8 @@ ebl_strtabinit (bool nullstr)
{
if (ps == 0)
{
- ps = sysconf (_SC_PAGESIZE) - 2 * sizeof (void *);
- assert (sizeof (struct memoryblock) < ps);
+ ps = sysconf (_SC_PAGESIZE);
+ assert (sizeof (struct memoryblock) < ps - MALLOC_OVERHEAD);
}
struct Ebl_Strtab *ret
@@ -135,8 +137,11 @@ ebl_strtabinit (bool nullstr)
static int
morememory (struct Ebl_Strtab *st, size_t len)
{
- if (len < ps)
- len = ps;
+ size_t overhead = offsetof (struct memoryblock, memory);
+ len += overhead + MALLOC_OVERHEAD;
+
+ /* Allocate nearest multiple of pagesize >= len. */
+ len = ((len / ps) + (len % ps != 0)) * ps - MALLOC_OVERHEAD;
struct memoryblock *newmem = (struct memoryblock *) malloc (len);
if (newmem == NULL)
@@ -145,7 +150,7 @@ morememory (struct Ebl_Strtab *st, size_t len)
newmem->next = st->memory;
st->memory = newmem;
st->backp = newmem->memory;
- st->left = len - offsetof (struct memoryblock, memory);
+ st->left = len - overhead;
return 0;
}