小さい頃はエラ呼吸

いつのまにやら肺で呼吸をしています。


【cppcheck】error: Common realloc mistake: 'tmp' nulled but not freed upon failure

はじめに

C++の静的解析ツール「cppcheck」でソースコードを静的解析した場合に、以下の警告がでることがあります。

error: Common realloc mistake: 'tmp' nulled but not freed upon failure

サンプルプログラム
#include "stdafx.h"
#include <Windows.h>

int _tmain(int argc, _TCHAR* argv[])
{
  char *tmp;

  // メモリアロケート
  tmp = (char*)malloc(100);
  if (tmp == NULL)
  {
    return 1;
  }

  //
  // 何か処理
  //

  // 再アロケート
  tmp = (char*)realloc(tmp, 100);
  if (tmp == NULL)
  {
    return 1;
  }

  // 解放
  free(tmp);
  
  return 0;
}
対処方法

以下の記事を参照してください。