aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--src/core/SkWriter32.cpp9
-rw-r--r--tests/Writer32Test.cpp5
2 files changed, 11 insertions, 3 deletions
diff --git a/src/core/SkWriter32.cpp b/src/core/SkWriter32.cpp
index 2fdb13376b..23f51b93fe 100644
--- a/src/core/SkWriter32.cpp
+++ b/src/core/SkWriter32.cpp
@@ -280,9 +280,12 @@ void SkWriter32::writeString(const char str[], size_t len) {
size_t alignedLen = SkAlign4(len + 1);
char* ptr = (char*)this->reserve(alignedLen);
memcpy(ptr, str, len);
- ptr[len] = 0;
- // we may have left 0,1,2,3 bytes uninitialized, since we reserved align4
- // number of bytes. That's ok, since the reader will know to skip those
+ // Add the terminating 0, and pad the rest with 0s
+ ptr += len;
+ int n = alignedLen - len;
+ while (--n >= 0) {
+ *ptr++ = 0;
+ }
}
size_t SkWriter32::WriteStringSize(const char* str, size_t len) {
diff --git a/tests/Writer32Test.cpp b/tests/Writer32Test.cpp
index 311f37d8fb..5c9d7ea58c 100644
--- a/tests/Writer32Test.cpp
+++ b/tests/Writer32Test.cpp
@@ -77,6 +77,11 @@ static void test2(skiatest::Reporter* reporter, SkWriter32* writer) {
REPORTER_ASSERT(reporter, i == len);
REPORTER_ASSERT(reporter, strlen(str) == len);
REPORTER_ASSERT(reporter, !memcmp(str, gStr, len));
+ // Ensure that the align4 of the string is padded with zeroes.
+ size_t alignedSize = SkAlign4(len + 1);
+ for (size_t j = len; j < alignedSize; j++) {
+ REPORTER_ASSERT(reporter, 0 == str[j]);
+ }
}
REPORTER_ASSERT(reporter, reader.eof());
}