Replace sk_malloc* and sk_realloc* with direct native calls
In addition, remove potential undefined behavior pertaining to calling realloc with size of zero. Change-Id: Ic39f368230e1075b37f9c67ec8c090e7d739e86c
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
#include "CreateJavaOutputStreamAdaptor.h"
|
||||
#include "SkData.h"
|
||||
#include "SkMalloc.h"
|
||||
#include "SkRefCnt.h"
|
||||
#include "SkStream.h"
|
||||
#include "SkTypes.h"
|
||||
#include "Utils.h"
|
||||
|
||||
#include <cstdlib>
|
||||
#include <nativehelper/JNIHelp.h>
|
||||
#include <log/log.h>
|
||||
#include <memory>
|
||||
@@ -177,6 +177,10 @@ SkStream* CreateJavaInputStreamAdaptor(JNIEnv* env, jobject stream, jbyteArray s
|
||||
return JavaInputStreamAdaptor::Create(env, stream, storage, swallowExceptions);
|
||||
}
|
||||
|
||||
static void free_pointer_skproc(const void* ptr, void*) {
|
||||
free((void*)ptr);
|
||||
}
|
||||
|
||||
sk_sp<SkData> CopyJavaInputStream(JNIEnv* env, jobject inputStream, jbyteArray storage) {
|
||||
std::unique_ptr<SkStream> stream(CreateJavaInputStreamAdaptor(env, inputStream, storage));
|
||||
if (!stream) {
|
||||
@@ -186,19 +190,31 @@ sk_sp<SkData> CopyJavaInputStream(JNIEnv* env, jobject inputStream, jbyteArray s
|
||||
size_t bufferSize = 4096;
|
||||
size_t streamLen = 0;
|
||||
size_t len;
|
||||
char* data = (char*)sk_malloc_throw(bufferSize);
|
||||
char* data = (char*)malloc(bufferSize);
|
||||
LOG_ALWAYS_FATAL_IF(!data);
|
||||
|
||||
while ((len = stream->read(data + streamLen,
|
||||
bufferSize - streamLen)) != 0) {
|
||||
streamLen += len;
|
||||
if (streamLen == bufferSize) {
|
||||
bufferSize *= 2;
|
||||
data = (char*)sk_realloc_throw(data, bufferSize);
|
||||
data = (char*)realloc(data, bufferSize);
|
||||
LOG_ALWAYS_FATAL_IF(!data);
|
||||
}
|
||||
}
|
||||
data = (char*)sk_realloc_throw(data, streamLen);
|
||||
|
||||
return SkData::MakeFromMalloc(data, streamLen);
|
||||
if (streamLen == 0) {
|
||||
// realloc with size 0 is unspecified behavior in C++11
|
||||
free(data);
|
||||
data = nullptr;
|
||||
} else {
|
||||
// Trim down the buffer to the actual size of the data.
|
||||
LOG_FATAL_IF(streamLen > bufferSize);
|
||||
data = (char*)realloc(data, streamLen);
|
||||
LOG_ALWAYS_FATAL_IF(!data);
|
||||
}
|
||||
// Just in case sk_free differs from free, we ask Skia to use
|
||||
// free to cleanup the buffer that SkData wraps.
|
||||
return SkData::MakeWithProc(data, streamLen, free_pointer_skproc, nullptr);
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
Reference in New Issue
Block a user