summaryrefslogtreecommitdiffstats
path: root/xml/src
diff options
context:
space:
mode:
authorAndroid (Google) Code Review <android-gerrit@google.com>2009-11-16 10:30:41 -0800
committerAndroid (Google) Code Review <android-gerrit@google.com>2009-11-16 10:30:41 -0800
commitd2bed869f45bd0f286f5916e58cdacde8bd66397 (patch)
tree699c2dd153e0f4db9b3b6f3ed9ec8a70ec5faf5e /xml/src
parent25c670d5f17355ac9c527a0f93a7783052618a7b (diff)
parent845ce3cbfd6972542b275c95eddfbb6e94469737 (diff)
downloadlibcore-d2bed869f45bd0f286f5916e58cdacde8bd66397.zip
libcore-d2bed869f45bd0f286f5916e58cdacde8bd66397.tar.gz
libcore-d2bed869f45bd0f286f5916e58cdacde8bd66397.tar.bz2
Merge change If8e2929a
* changes: Don't allocate arbitrary-length buffers on the stack.
Diffstat (limited to 'xml/src')
-rw-r--r--xml/src/main/native/org_apache_harmony_xml_ExpatParser.cpp35
1 files changed, 15 insertions, 20 deletions
diff --git a/xml/src/main/native/org_apache_harmony_xml_ExpatParser.cpp b/xml/src/main/native/org_apache_harmony_xml_ExpatParser.cpp
index 9192b1a..701dbd9 100644
--- a/xml/src/main/native/org_apache_harmony_xml_ExpatParser.cpp
+++ b/xml/src/main/native/org_apache_harmony_xml_ExpatParser.cpp
@@ -16,9 +16,10 @@
#define LOG_TAG "ExpatParser"
+#include "JNIHelp.h"
+#include "LocalArray.h"
#include "jni.h"
#include "utils/Log.h"
-#include "JNIHelp.h"
#include <string.h>
#include <utils/misc.h>
@@ -1242,30 +1243,24 @@ static jint getAttributeIndexForQName(JNIEnv* env, jobject clazz,
static jint getAttributeIndex(JNIEnv* env, jobject clazz,
jint attributePointer, jstring uri, jstring localName) {
const char** attributes = (const char**) attributePointer;
- int uriLength = env->GetStringUTFLength(uri);
-
- if (uriLength == 0) {
+ int uriByteCount = env->GetStringUTFLength(uri);
+ if (uriByteCount == 0) {
// If there's no URI, then a local name works just like a qName.
return getAttributeIndexForQName(
env, clazz, attributePointer, localName);
}
- int localNameLength = env->GetStringUTFLength(localName);
-
- // Create string in the same format used by Expat: "uri|localName"
- // TODO: do we have a guarantee that uriLength and localNameLength are small?
- char concatenated[uriLength + localNameLength + 2];
-
- // Append uri.
- env->GetStringUTFRegion(uri, 0, uriLength, concatenated);
-
- // Separator.
- concatenated[uriLength] = '|';
-
- // Append local name.
- env->GetStringUTFRegion(localName, 0, localNameLength,
- concatenated + uriLength + 1);
- return findAttributeByName(attributes, concatenated);
+ // Create string in the same format used by Expat: "uri|localName\0".
+ // Note that we need byte counts to size the array but Unicode char counts
+ // for GetStringUTFRegion indexes and counts.
+ int localNameByteCount = env->GetStringUTFLength(localName);
+ LocalArray<1024> concatenated(uriByteCount + 1 + localNameByteCount + 1);
+ env->GetStringUTFRegion(uri, 0, env->GetStringLength(uri), &concatenated[0]);
+ concatenated[uriByteCount] = '|';
+ env->GetStringUTFRegion(localName, 0, env->GetStringLength(localName),
+ &concatenated[uriByteCount + 1]);
+
+ return findAttributeByName(attributes, &concatenated[0]);
}
/**