aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar commit-bot@chromium.org <commit-bot@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81>2013-08-09 16:03:05 +0000
committerGravatar commit-bot@chromium.org <commit-bot@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81>2013-08-09 16:03:05 +0000
commit47fa13640b7c8615aa7aee0d5b1d63a7bd6ed44e (patch)
tree2b9635a803586496083420c7b35c35a12418abce
parentee05f759c8528dc251adf51b83278f1d05b6443e (diff)
allow NULL in writeString/readString
BUG=skia:1469, code.google.com/p/android/issues/detail?id=58257 R=scroggo@google.com Author: mtklein@google.com Review URL: https://chromiumcodereview.appspot.com/22359003 git-svn-id: http://skia.googlecode.com/svn/trunk@10662 2bbb7eff-a529-9590-31e7-b0007b416f81
-rw-r--r--src/core/SkWriter32.cpp12
-rw-r--r--tests/Writer32Test.cpp29
2 files changed, 40 insertions, 1 deletions
diff --git a/src/core/SkWriter32.cpp b/src/core/SkWriter32.cpp
index 56edd19698..e41e2df0c4 100644
--- a/src/core/SkWriter32.cpp
+++ b/src/core/SkWriter32.cpp
@@ -246,6 +246,12 @@ void SkWriter32::validate() const {
const char* SkReader32::readString(size_t* outLen) {
size_t len = this->readInt();
+ if (0xFFFF == len) {
+ if (outLen) {
+ *outLen = 0;
+ }
+ return NULL;
+ }
const void* ptr = this->peek();
// skip over teh string + '\0' and then pad to a multiple of 4
@@ -268,8 +274,12 @@ size_t SkReader32::readIntoString(SkString* copy) {
}
void SkWriter32::writeString(const char str[], size_t len) {
+ if (NULL == str) {
+ // We're already requiring len < 0xFFFF, so we can use that to mark NULL.
+ this->write32(0xFFFF);
+ return;
+ }
if ((long)len < 0) {
- SkASSERT(str);
len = strlen(str);
}
this->write32(len);
diff --git a/tests/Writer32Test.cpp b/tests/Writer32Test.cpp
index e5b93634ac..6ecfcf3eda 100644
--- a/tests/Writer32Test.cpp
+++ b/tests/Writer32Test.cpp
@@ -21,6 +21,34 @@ static void check_contents(skiatest::Reporter* reporter, const SkWriter32& write
REPORTER_ASSERT(reporter, !memcmp(storage.get(), expected, size));
}
+static void test_string_null(skiatest::Reporter* reporter) {
+ uint8_t storage[8];
+ SkWriter32 writer(0, storage, sizeof(storage));
+ SkReader32 reader(storage, sizeof(storage));
+
+ const char* str;
+ size_t len;
+
+ // Can we write NULL?
+ writer.writeString(NULL);
+ const int32_t null[] = { 0xFFFF };
+ check_contents(reporter, writer, null, sizeof(null));
+ str = reader.readString(&len);
+ REPORTER_ASSERT(reporter, NULL == str);
+ REPORTER_ASSERT(reporter, 0 == len);
+
+ writer.reset(storage, sizeof(storage));
+ reader.rewind();
+
+ // Is NULL distinct from ""?
+ writer.writeString("");
+ const int32_t empty[] = { 0x0, 0x0 };
+ check_contents(reporter, writer, empty, sizeof(empty));
+ str = reader.readString(&len);
+ REPORTER_ASSERT(reporter, 0 == strcmp("", str));
+ REPORTER_ASSERT(reporter, 0 == len);
+}
+
static void test_rewind(skiatest::Reporter* reporter) {
SkSWriter32<32> writer(32);
int32_t array[3] = { 1, 2, 4 };
@@ -228,6 +256,7 @@ static void Tests(skiatest::Reporter* reporter) {
testWritePad(reporter, &writer);
}
+ test_string_null(reporter);
test_ptr(reporter);
test_rewind(reporter);
}