Cocos2d-ãªãŒãã³ãœãŒã¹ãœãããŠã§ã¢ããã¬ãŒã ã¯ãŒã¯ã ã€ã³ã¿ã©ã¯ãã£ããªã¯ãã¹ãã©ãããã©ãŒã ã¢ããªã±ãŒã·ã§ã³ã®ã²ãŒã ãã¢ããªã±ãŒã·ã§ã³ãã°ã©ãã£ã«ã«ã€ã³ã¿ãŒãã§ã€ã¹ãæ§ç¯ããããã«äœ¿çšã§ããŸãã Cocos2dã«ã¯å€ãã®ãã©ã³ããå«ãŸããŠããŸããæåãªãã®ã¯Cocos2d-iPhoneãCocos2d-xãCocos2d-html5ãããã³Cocos2d-XNAã§ãã
ãã®èšäºã§ã¯ã PVS-Studio 5.18ã䜿çšããŠååŸãããC ++ã®ãã¬ãŒã ã¯ãŒã¯ã§ããCocos2d-xã®ãã§ãã¯çµæã調ã¹ãŸãã ãããžã§ã¯ãã¯éåžžã«é«å質ã§ãããããã§ãããã€ãã®å Žæã«æ³šæãæã䟡å€ããããŸãã ãœãŒã¹ã³ãŒãã¯GitHubããååŸãããŸã ã
mallocããnewãžãCããC ++ãž
ååãšããŠãã°ã©ãã£ãã¯ãªããžã§ã¯ãã®æäœã¯ãã¡ã¢ãªãåçã«å²ãåœãŠãããé åãšè¡åã®åŠçã«é¢é£ä»ããããŸãã ãã®ãããžã§ã¯ãã§ã¯ãmallocé¢æ°åŒã³åºããšnewæŒç®åã®äž¡æ¹ã䜿çšããŠã¡ã¢ãªãå²ãåœãŠãŸãã ãããã®ã¡ãœããã¯ãã³ãŒãå ã§ã¡ãœããã眮ãæãããšãã«èæ ®ããå¿ èŠããã䜿çšäžã®å€§ããªéãããããŸãã 以äžã§ã¯ããmallocããšãnewãããŸã£ããæ£ãã䜿çšããŠããªãå Žæã«ã€ããŠèª¬æããŸãã
V630 ãmallocãé¢æ°ã¯ãã³ã³ã¹ãã©ã¯ã¿ãŒãšãã¹ãã©ã¯ã¿ãŒãå«ãã¯ã©ã¹ã§ãããªããžã§ã¯ãã®é åã«ã¡ã¢ãªãå²ãåœãŠãããã«äœ¿çšãããŸãã ccmotionstreak.cpp 122
Vec2::Vec2() : x(0.0f), y(0.0f) { } Vec2::Vec2(float xx, float yy) : x(xx), y(yy) { } bool MotionStreak::initWithFade(...) { .... _pointVertexes = (Vec2*)malloc(sizeof(Vec2) * _maxPoints); _vertices = (Vec2*)malloc(sizeof(Vec2) * _maxPoints * 2); _texCoords = (Tex2F*)malloc(sizeof(Tex2F) * _maxPoints * 2); .... }
éåžžãå°çšã¡ã¢ãªã¯ãã³ã³ã¹ãã©ã¯ã¿ãŸãã¯ãã¹ãã©ã¯ã¿ãæã€ãªããžã§ã¯ãã®é åã®ããã«æ±ãããŸãã ãã®ã¯ã©ã¹ã®ã¡ã¢ãªã®å²ãåœãŠã§ã¯ãã³ã³ã¹ãã©ã¯ã¿ãŒã¯åŒã³åºãããŸããã freeé¢æ°ã䜿çšããŠã¡ã¢ãªã解æŸããå Žåããã¹ãã©ã¯ã¿ãåŒã³åºãããŸããã ããã¯éåžžã«çãããã§ãã ãã®çµæãå€æ°ãxãããã³ãyãã¯åæåãããŸããã ãã¡ãããåãªããžã§ã¯ãã®ã³ã³ã¹ãã©ã¯ã¿ãŒããæåã§ãåŒã³åºãããšãããã£ãŒã«ããæ瀺çã«åæåããããšãã§ããŸããããnewãæŒç®åã䜿çšããæ¹ãããé©åã§ãã
_pointVertexes = new Vec2[ _maxPoints]; _vertices = new Vec2[_maxPoints * 2];
åæ§ã®å ŽæïŒ
- V630ãmallocãé¢æ°ã¯ãã³ã³ã¹ãã©ã¯ã¿ãŒãšãã¹ãã©ã¯ã¿ãŒãå«ãã¯ã©ã¹ã§ãããªããžã§ã¯ãã®é åã«ã¡ã¢ãªãå²ãåœãŠãããã«äœ¿çšãããŸãã ccmotionstreak.cpp 124
- V630ãmallocãé¢æ°ã¯ãã³ã³ã¹ãã©ã¯ã¿ãŒãå«ãã¯ã©ã¹ã§ãããªããžã§ã¯ãã®é åã«ã¡ã¢ãªãå²ãåœãŠãããã«äœ¿çšãããŸãã ccmotionstreak.cpp 125
V572 ãnewãæŒç®åã䜿çšããŠäœæããããªããžã§ã¯ããããã«å¥ã®åã«ãã£ã¹ããããã®ã¯å¥åŠã§ãã ccactiontiledgrid.cpp 322
struct Tile { Vec2 position; Vec2 startPosition; Size delta; }; Tile* _tiles; void ShuffleTiles::startWithTarget(Node *target) { .... _tiles = (struct Tile *)new Tile[_tilesCount]; //<== Tile *tileArray = (Tile*) _tiles; //<== .... }
ããã§ããnewãæŒç®åã¯æ¢ã«åä»ããã€ã³ã¿ãè¿ãããããåãåã«ãã£ã¹ãããŠãæå³ããããŸããã
åæ§ã®å ŽæïŒ
- V572ãnewãæŒç®åã䜿çšããŠäœæããããªããžã§ã¯ããããã«å¥ã®åã«ãã£ã¹ããããã®ã¯å¥åŠã§ãã luabasicconversions.cpp 1301
V668ã¡ã¢ãªã¯ãnewãæŒç®åã䜿çšããŠå²ãåœãŠãããããããpRetããã€ã³ã¿ãnullã«å¯ŸããŠãã¹ãããæå³ã¯ãããŸããã ã¡ã¢ãªå²ãåœãŠãšã©ãŒã®å ŽåãäŸå€ãçæãããŸãã ccfloat.h 48
static __Float* create(float v) { __Float* pRet = new __Float(v); //<== if (pRet) //<== { pRet->autorelease(); } return pRet; }
ãnewãæŒç®åãã¡ã¢ãªãå²ãåœãŠãããªãã£ãå ŽåãC ++èšèªæšæºã«åŸã£ãŠãäŸå€std :: bad_allocïŒïŒãã¹ããŒãããŸãã ãããã£ãŠã 'malloc'é¢æ°ã®æ»ãå€ãšã¯ç°ãªãããã€ã³ã¿ãŒããŒãã«çãããã©ããããã§ãã¯ããããšã¯æå³ããããŸããã ãããžã§ã¯ãã«ã¯ããã«475åã®ãã®ãããªãã§ãã¯ããããŸãïŒ
V547åŒ '0 == commonInfo-> eventName'ã¯åžžã«falseã§ãã ãã€ã³ã¿ãŒ 'commonInfo-> eventName'ïŒ= NULLã ccluaengine.cpp 436
struct CommonScriptData { // Now this struct is only used in LuaBinding. int handler; char eventName[64]; //<== .... }; int LuaEngine::handleCommonEvent(void* data) { .... CommonScriptData* commonInfo = static_cast<....*>(data); if (NULL == commonInfo->eventName || //<== 0 == commonInfo->handler) return 0; .... }
é åãeventNameãã¯ããŒã«ã«ã§å®£èšãããŠãããããæ¡ä»¶ïŒNULL == commonInfo-> eventNameïŒã¯åžžã«falseã«ãªããŸãã åºå®ãµã€ãºã®é åã«ã¡ã¢ãªãå²ãåœãŠãããšãã§ããªãå Žåãåé¡ã¯ä»¥åã«ç¹å®ãããŸã-æ§é ã«ã¡ã¢ãªãå²ãåœãŠããšãã
ãã®ä»ã®ãã§ãã¯ïŒ
- V547åŒ '0ïŒ= CommonInfo-> eventSourceClassName'ã¯åžžã«trueã§ãã ãã€ã³ã¿ãŒ 'commonInfo-> eventSourceClassName'ïŒ= NULLã ccluaengine.cpp 442
- V600ç¶æ ã®æ€æ»ãæ€èšããŠãã ããã 'commonInfo-> eventName'ãã€ã³ã¿ãŒã¯åžžã«NULLãšçãããããŸããã ccluaengine.cpp 436
- V600ç¶æ ã®æ€æ»ãæ€èšããŠãã ããã 'commonInfo-> eventSourceClassName'ãã€ã³ã¿ãŒã¯åžžã«NULLãšçãããããŸããã ccluaengine.cpp 442
æ§é ããã°ã©ãã³ã°ã®æªå€¢
V696 ãcontinueãæŒç®åã¯ãæ¡ä»¶ãåžžã«falseã§ããããããdo {...} whileïŒFALSEïŒãã«ãŒããçµäºããŸãã ãã§ãã¯è¡ïŒ125ã153ãcccomaudio.cpp 125
bool ComAudio::serialize(void* r) { bool ret = false; do { .... if (file != nullptr) { if (strcmp(file, "") == 0) { continue; //<== } .... } }while(0); return ret; }
ã¢ãã©ã€ã¶ãŒã¯ãããã°ã©ããŒã誀解ãããå¯èœæ§ã®ããã³ãŒããæ€åºããŸããã "do {...} whileïŒ0ïŒ"ã«ãŒãã®continueã¹ããŒãã¡ã³ãã¯ãã«ãŒããåéããã®ã§ã¯ãªããåæ¢ããŸãã ãããã£ãŠã 'continue'ã¹ããŒãã¡ã³ããåŒã³åºããåŸãæ¡ä»¶ïŒ0ïŒããã§ãã¯ãããæ¡ä»¶ãfalseã§ãããããµã€ã¯ã«ãçµäºããŸãã ãããæå³ãããšããã§ããšã©ãŒããªãå Žåã¯ãã³ãŒããå€æŽããããšããå§ãããŸãã ãbreakãæŒç®åã䜿çšã§ããŸãã
åæ§ã®ã«ãŒãïŒ
- V696ãcontinueãæŒç®åã¯ãæ¡ä»¶ãåžžã«falseã§ããããããdo {...} whileïŒFALSEïŒãã«ãŒããçµäºããŸãã è¡ã確èªããŠãã ããïŒ188ã341ãcccomrender.cpp 188
- V696ãcontinueãæŒç®åã¯ãæ¡ä»¶ãåžžã«falseã§ããããããdo {...} whileïŒFALSEïŒãã«ãŒããçµäºããŸãã è¡ã確èªããŠãã ããïŒ276ã341ãcccomrender.cpp 276
- V696ãcontinueãæŒç®åã¯ãæ¡ä»¶ãåžžã«falseã§ããããããdo {...} whileïŒFALSEïŒãã«ãŒããçµäºããŸãã è¡ã確èªïŒ281ã341ãcccomrender.cpp 281
- V696ãcontinueãæŒç®åã¯ãæ¡ä»¶ãåžžã«falseã§ããããããdo {...} whileïŒFALSEïŒãã«ãŒããçµäºããŸãã è¡ã確èªïŒ323ã341ãcccomrender.cpp 323
ãã©ãŒããããããåºå
V576圢åŒãæ£ãããããŸãã ã 'fprintf'é¢æ°ã®4çªç®ã®å®åŒæ°ã確èªããããšãæ€èšããŠãã ããã charåã®ã·ã³ãã«ã®æååãžã®ãã€ã³ã¿ãå¿ èŠã§ãã ccconsole.cpp 341
#ifdef UNICODE #define gai_strerror gai_strerrorW //<== #else #define gai_strerror gai_strerrorA #endif /* UNICODE */ bool Console::listenOnTCP(int port) { .... fprintf(stderr,"net_listen error for %s: %s", //<== serv, gai_strerror(n)); //<== .... }
gai_strerroré¢æ°ã¯ãUNICODEãã£ã¬ã¯ãã£ãã®å®çŸ©ã«å¿ããŠãgai_strerrorWããã³gai_strerrorAãšããŠå®çŸ©ã§ããŸãã ãããžã§ã¯ãããã¹ããããVisual Studio 2012ã§ã¯ã 'ïŒ S'ïŒå€§ããªSïŒæå®åã䜿çšããå¿ èŠãããå°å·çšã®ã¯ã€ãæååãè¿ãUnicodeé¢æ°ãå®çŸ©ãããŸãããããã§ãªãå Žåãè¡ã®æåã®æåãŸãã¯ç¡æå³ãªããã¹ãã®ã¿ãå°å·ãããŸãã
æ¡ä»¶ã®åãçµæ
V583 ãïŒïŒãæŒç®åã¯ããã®æ¡ä»¶åŒã«é¢ä¿ãªããåžžã«1ã€ã®åãå€ATLAS_REPEATãè¿ããŸãã atlas.cpp 219
spAtlas* spAtlas_readAtlas (....) { .... page->uWrap = *str.begin == 'x' ? ATLAS_REPEAT : (*str.begin == 'y' ? ATLAS_CLAMPTOEDGE : ATLAS_REPEAT); page->vWrap = *str.begin == 'x' ? ATLAS_CLAMPTOEDGE : (*str.begin == 'y' ? ATLAS_REPEAT : ATLAS_REPEAT); //<== .... }
ãããããããã¯çŸããã®ããã«æžãããã®ãããããŸããããããã§ããããæ¡ä»¶ã§1ã€ã®å€ãè¿ãããããšã¯éåžžã«çãããããã§ãã
ãã€ã³ã¿ãŒã®éåç §
V595 nullptrã«å¯ŸããŠæ€èšŒãããåã«ããå€ããã€ã³ã¿ãŒã䜿çšãããŸããã è¡ã確èªããŠãã ããïŒ188ã189ãccbundlereader.h 188
template<> inline bool BundleReader::readArray<std::string>( unsigned int *length, std::vector<std::string> *values) { .... values->clear(); //<== if (*length > 0 && values) //<== { for (int i = 0; i < (int)*length; ++i) { values->push_back(readString()); } } return true; }
ã³ãŒãå ã®å€ãã®å Žæã§ãæåéãéæ¥åç §ã®çŽåŸã«ããã€ã³ã¿ãŒã®æå¹æ§ããã§ãã¯ãããŸãã ãããã®å Žæã®ããã€ãã次ã«ç€ºããŸãã
- V595 nullptrã«å¯ŸããŠæ€èšŒãããåã«ã '_ openGLView'ãã€ã³ã¿ãŒã䜿çšãããŸããã è¡ã確èªããŠãã ããïŒ410ã417ãccdirector.cpp 410
- V595 nullptrã«å¯ŸããŠæ€èšŒãããåã«ããããŒãããã€ã³ã¿ãŒã䜿çšãããŸããã è¡ã確èªããŠãã ããïŒ365ã374ãcctween.cpp 365
- V595 nullptrã«å¯ŸããŠæ€èšŒãããåã«ããrootEleããã€ã³ã¿ãŒã䜿çšãããŸããã è¡ã確èªããŠãã ããïŒ378ã379ãccfileutils.cpp 378
- V595 nullptrã«å¯ŸããŠæ€èšŒãããåã«ããtolua_retããã€ã³ã¿ãŒã䜿çšãããŸããã è¡ã確èªããŠãã ããïŒ429ã433ãlua_cocos2dx_manual.cpp 429
- V595 nullptrã«å¯ŸããŠæ€èšŒãããåã«ããtolua_retããã€ã³ã¿ãŒã䜿çšãããŸããã è¡ã確èªããŠãã ããïŒ1858ã1861ãlua_cocos2dx_manual.cpp 1858
- V595 nullptrã«å¯ŸããŠæ€èšŒãããåã«ããtolua_retããã€ã³ã¿ãŒã䜿çšãããŸããã è¡ã確èªããŠãã ããïŒ4779ã4781ãlua_cocos2dx_manual.cpp 4779
- V595 nullptrã«å¯ŸããŠæ€èšŒãããåã«ã '_ fontAtlas'ãã€ã³ã¿ãŒã䜿çšãããŸããã è¡ã確èªïŒ384ã396ãcclabel.cpp 384
- V595 nullptrã«å¯ŸããŠæ€èšŒãããåã«ã '_ glprogramstate'ãã€ã³ã¿ãŒã䜿çšãããŸããã è¡ã確èªããŠãã ããïŒ216ã218ãshadertest2.cpp 216
- V595 nullptrã«å¯ŸããŠæ€èšŒãããåã«ã '_ sprite'ãã€ã³ã¿ãŒã䜿çšãããŸããã è¡ã確èªããŠãã ããïŒ530ã533ãsprite3dtest.cpp 530
ã©ã³ãã ãã¹ã
V636 ãrandïŒïŒ/ 0x7fffãåŒã¯ãæé»çã«ãintãåãããfloatãåã«ãã£ã¹ããããŸããã åæ°éšåã®æ倱ãé¿ããããã«ãæ瀺çãªåãã£ã¹ãã®äœ¿çšãæ€èšããŠãã ããã äŸïŒdouble A =ïŒdoubleïŒïŒXïŒ/ Y;ã cpp-tests physicstest.cpp 307
static inline float frand(void) { return rand()/RAND_MAX; }
ãã¹ãçšã®ãœãŒã¹ãã¡ã€ã«ã§ããã®ãããªæ©èœãçºèŠãããŸããã ã»ãšãã©ã®å Žåã0.0fãã1.0fã®ç¯å²ã®å®æ°ãååŸããäºå®ã§ãããrandïŒïŒé¢æ°ã®çµæã¯æŽæ°ã§ãããããå®æ°éšã¯é€ç®åŸã«ç Žæ£ãããŸãã é¢æ°ã¯0.0ãŸãã¯1.0ã®ã¿ãè¿ããŸãã ããã«ãrandïŒïŒé¢æ°ã¯0ããRAND_MAXã®å€ãè¿ããããçµæã1.0ã«ãªã確çã¯ãããããã§ãã
ããããfrandïŒïŒé¢æ°ã䜿çšãããã¹ãã¯å®éã«ã¯äœããã¹ãããŸããã éçåæãåäœãã¹ãã§ãã¹ããè£å®ããæ¹æ³ã®è¯ãäŸã§ãã
ãããã«
æåã«èšã£ãããã«ãCocos2d-xã«ã¯ããªãã®æ°ã®çãããå Žæãå«ãŸããŠããŸãã ãã®ãããžã§ã¯ãã¯æ¯èŒçæ°ããé©æ°çã§ãæããç¶æ¿ãããã³ãŒãã¯å«ãŸããŠããŸããã 確ãã«éçºè ã¯å質管çã®ããŸããŸãªæ¹æ³ã䜿çšããçŸä»£ã®æšæºãšããã°ã©ãã³ã°æ¹æ³è«ã«åŸãããã«ããŠããŸãã
ãã®èšäºã¯è±èªã§ãã
è±èªã話ãèŽè¡ãšãã®èšäºãå ±æãããå Žåã¯ã翻蚳ãžã®ãªã³ã¯ã䜿çšããŠãã ããïŒSvyatoslav Razmyslovã ã¯ãã¹ãã©ãããã©ãŒã ãã¬ãŒã ã¯ãŒã¯Cocos2d-xã®ç¢ºèª ã
èšäºãèªãã§è³ªåããããŸããïŒ
å€ãã®å Žåãèšäºã«ã¯åã質åãå¯ããããŸãã ããã§ãããã«å¯ŸããåçãåéããŸããïŒ PVS-Studioããã³CppCatããŒãžã§ã³2014ã«é¢ããèšäºã®èªè
ããã®è³ªåãžã®åç ã ãªã¹ããã芧ãã ããã