From 6fceca940fd363827350d5e8348be52686372212 Mon Sep 17 00:00:00 2001 From: Joel Dice Date: Wed, 29 Feb 2012 11:49:13 -0700 Subject: [PATCH] fix makeByteArray and makeString for strings longer than 256 characters --- src/machine.cpp | 59 +++++++++++++++++++++++++++++-------------------- src/machine.h | 2 +- 2 files changed, 36 insertions(+), 25 deletions(-) diff --git a/src/machine.cpp b/src/machine.cpp index f05ba92d39..0ff4539bf6 100644 --- a/src/machine.cpp +++ b/src/machine.cpp @@ -3048,17 +3048,32 @@ popResources(Thread* t) } object -makeByteArray(Thread* t, const char* format, va_list a) +makeByteArray(Thread* t, const char* format, va_list a, int size) +{ + THREAD_RUNTIME_ARRAY(t, char, buffer, size); + + int r = vm::vsnprintf(RUNTIME_ARRAY_BODY(buffer), size - 1, format, a); + if (r >= 0 and r < size - 1) { + object s = makeByteArray(t, strlen(RUNTIME_ARRAY_BODY(buffer)) + 1); + memcpy(&byteArrayBody(t, s, 0), RUNTIME_ARRAY_BODY(buffer), + byteArrayLength(t, s)); + return s; + } else { + return 0; + } +} + +object +makeByteArray(Thread* t, const char* format, ...) { int size = 256; while (true) { - THREAD_RUNTIME_ARRAY(t, char, buffer, size); - - int r = vm::vsnprintf(RUNTIME_ARRAY_BODY(buffer), size - 1, format, a); - if (r >= 0 and r < size - 1) { - object s = makeByteArray(t, strlen(RUNTIME_ARRAY_BODY(buffer)) + 1); - memcpy(&byteArrayBody(t, s, 0), RUNTIME_ARRAY_BODY(buffer), - byteArrayLength(t, s)); + va_list a; + va_start(a, format); + object s = makeByteArray(t, format, a, size); + va_end(a); + + if (s) { return s; } else { size *= 2; @@ -3066,26 +3081,22 @@ makeByteArray(Thread* t, const char* format, va_list a) } } -object -makeByteArray(Thread* t, const char* format, ...) -{ - va_list a; - va_start(a, format); - object s = makeByteArray(t, format, a); - va_end(a); - - return s; -} - object makeString(Thread* t, const char* format, ...) { - va_list a; - va_start(a, format); - object s = makeByteArray(t, format, a); - va_end(a); + int size = 256; + while (true) { + va_list a; + va_start(a, format); + object s = makeByteArray(t, format, a, size); + va_end(a); - return t->m->classpath->makeString(t, s, 0, byteArrayLength(t, s) - 1); + if (s) { + return t->m->classpath->makeString(t, s, 0, byteArrayLength(t, s) - 1); + } else { + size *= 2; + } + } } int diff --git a/src/machine.h b/src/machine.h index 83ea2577a4..8f2a6f4cf7 100644 --- a/src/machine.h +++ b/src/machine.h @@ -2214,7 +2214,7 @@ make(Thread* t, object class_) } object -makeByteArray(Thread* t, const char* format, va_list a); +makeByteArray(Thread* t, const char* format, va_list a, int size); object makeByteArray(Thread* t, const char* format, ...);