From 3264042873fd639f3ef8ff0acfad777a0a9f3355 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 28 Apr 2011 14:24:16 -0400 Subject: [PATCH] [test/buffer] Test pre_allocate() and allocation_successful() --- src/hb-buffer.cc | 3 ++- test/test-buffer.c | 26 +++++++++++++++++++++++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/hb-buffer.cc b/src/hb-buffer.cc index 7dfe83a..13d0040 100644 --- a/src/hb-buffer.cc +++ b/src/hb-buffer.cc @@ -80,7 +80,8 @@ _hb_buffer_enlarge (hb_buffer_t *buffer, unsigned int size) bool overflows = FALSE; bool separate_out = buffer->out_info != buffer->info; - if (unlikely (size > ((unsigned int)-1) / 2)) + overflows = size >= ((unsigned int) -1) / sizeof (buffer->info[0]); + if (unlikely (overflows)) goto done; while (size > new_allocated) diff --git a/test/test-buffer.c b/test/test-buffer.c index b79f955..61a1b48 100644 --- a/test/test-buffer.c +++ b/test/test-buffer.c @@ -281,10 +281,35 @@ test_buffer_allocation (fixture_t *fixture, gconstpointer user_data) g_assert (hb_buffer_pre_allocate (fixture->b, 100)); g_assert_cmpint (hb_buffer_get_length (fixture->b), ==, 0); + g_assert (hb_buffer_allocation_successful (fixture->b)); /* lets try a huge allocation, make sure it fails */ g_assert (!hb_buffer_pre_allocate (fixture->b, (unsigned int) -1)); g_assert_cmpint (hb_buffer_get_length (fixture->b), ==, 0); + g_assert (!hb_buffer_allocation_successful (fixture->b)); + + /* small one again */ + g_assert (hb_buffer_pre_allocate (fixture->b, 50)); + g_assert_cmpint (hb_buffer_get_length (fixture->b), ==, 0); + g_assert (!hb_buffer_allocation_successful (fixture->b)); + + hb_buffer_reset (fixture->b); + g_assert (hb_buffer_allocation_successful (fixture->b)); + + /* all allocation and size */ + g_assert (!hb_buffer_pre_allocate (fixture->b, ((unsigned int) -1) / 20 + 1)); + g_assert (!hb_buffer_allocation_successful (fixture->b)); + + hb_buffer_reset (fixture->b); + g_assert (hb_buffer_allocation_successful (fixture->b)); + + /* technically, this one can actually pass on 64bit machines, but + * I'm doubtful that any malloc allows 4GB allocations at a time. */ + g_assert (!hb_buffer_pre_allocate (fixture->b, ((unsigned int) -1) / 20 - 1)); + g_assert (!hb_buffer_allocation_successful (fixture->b)); + + hb_buffer_reset (fixture->b); + g_assert (hb_buffer_allocation_successful (fixture->b)); } @@ -311,7 +336,6 @@ main (int argc, char **argv) g_test_add ("/buffer/allocation", fixture_t, GINT_TO_POINTER (BUFFER_EMPTY), fixture_init, test_buffer_allocation, fixture_fini); /* XXX test invalid UTF-8 / UTF-16 text input (also overlong sequences) */ - /* XXX test pre_allocate(), allocation_successful(), and memory management */ return g_test_run(); } -- 2.7.4