From e5b14733c8765c0848282cc8137ae4ac77c8cb7e Mon Sep 17 00:00:00 2001 From: Ricardo Quesada Date: Sun, 7 Aug 2016 18:47:05 -0700 Subject: [PATCH] from spine source code (#16330) Related to: * Github issue #16263 * Github issue #16294 --- cocos/editor-support/spine/spine-cocos2dx.cpp | 23 +++++++++++++------ 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/cocos/editor-support/spine/spine-cocos2dx.cpp b/cocos/editor-support/spine/spine-cocos2dx.cpp index 5a24d4ca7c..da7d3fa6d2 100644 --- a/cocos/editor-support/spine/spine-cocos2dx.cpp +++ b/cocos/editor-support/spine/spine-cocos2dx.cpp @@ -62,6 +62,7 @@ GLuint filter (spAtlasFilter filter) { void _spAtlasPage_createTexture (spAtlasPage* self, const char* path) { Texture2D* texture = Director::getInstance()->getTextureCache()->addImage(path); + CCASSERT(texture != nullptr, "Invalid image"); texture->retain(); Texture2D::TexParams textureParams = {filter(self->minFilter), filter(self->magFilter), wrap(self->uWrap), wrap(self->vWrap)}; @@ -77,11 +78,19 @@ void _spAtlasPage_disposeTexture (spAtlasPage* self) { } char* _spUtil_readFile (const char* path, int* length) { - Data data = FileUtils::getInstance()->getDataFromFile( - FileUtils::getInstance()->fullPathForFilename(path).c_str()); - if (data.isNull()) return 0; - *length = static_cast(data.getSize()); - char* bytes = MALLOC(char, *length); - memcpy(bytes, data.getBytes(), *length); - return bytes; + Data data = FileUtils::getInstance()->getDataFromFile(FileUtils::getInstance()->fullPathForFilename(path)); + if (data.isNull()) return 0; + + // avoid buffer overflow (int is shorter than ssize_t in certain platforms) +#if COCOS2D_VERSION >= 0x00031200 + ssize_t tmpLen; + char *ret = (char*)data.takeBuffer(&tmpLen); + *length = static_cast(tmpLen); + return ret; +#else + *length = static_cast(data.getSize()); + char* bytes = MALLOC(char, *length); + memcpy(bytes, data.getBytes(), *length); + return bytes; +#endif }