Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

False positive static analyser?

I am running MSVC’s static analyzer on my code, and I’m receiving a warning that I cannot solve. I’m not sure whether this is a false positive or not and hopefully, you can help me figure out this issue.
I’m using MSVC17.

void GetParam_MEASLIST(Message* pMsgReturn)
{
   pMsgReturn->u8MessageType = MESSAGE_FAILED;

   uint32_t u32ChannelCount = 0;
   u32ChannelCount = ChannelHandler_getChannelCount();
   if (u32ChannelCount == 0)
   {
      return;
   }

   char** szStringList = (char**)malloc(u32ChannelCount * sizeof(char*));
   if (NULL == szStringList)
   {
      return;
   }

   for (uint32_t i = 0; i < u32ChannelCount; ++i)
   {
      ChannelInfo_t tmp = { 0 };
      if (!ChannelHandler_getChannelInfo(i, &tmp))
      {
         LOG_ERROR("Failed to get channel info for channel %i", mg_strLogCat, 10);
      }
      else
      {
         size_t nSize = strlen(tmp.ChannelDeviceInfo.szMeasType) + 1;
         if (nSize > 1)
         {
            szStringList[i] = (char*)malloc(nSize);
            if (szStringList[i] != NULL)
            {
               memset(szStringList[i], 0, nSize);
               memcpy(szStringList[i], tmp.ChannelDeviceInfo.szMeasType, nSize);
            }
         }
         else
         {
            szStringList[i] = NULL;
         }
      }
   }
   size_t nSizeOfList = sizeofStringList((const char**)szStringList, u32ChannelCount);
   pMsgReturn->pData = malloc(nSizeOfList);
   if (pMsgReturn->pData != NULL)
   {
      pMsgReturn->nDataSize = serializeStringList(pMsgReturn->pData, (const char**)szStringList, u32ChannelCount);
      pMsgReturn->bFreeDataAfterSend = true;
      pMsgReturn->u8MessageType = MESSAGE_SUCCESS;
   }
   for (uint32_t i = 0; i < u32ChannelCount; i++)
   {
      if (NULL != szStringList[i]) //< error szStringList is uninitialised
      {
         free(szStringList[i]);
      }
   }
   free(szStringList);
}

I receive an uninitialized error on the szStringList variable, but I cannot figure out why. Could you help me solve this issue?

Edit:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

As requested the full error

warning C6001: uninitialized memory "*szStringList" is used.: Lines: 219, 221, 222, 223, 228, 229, 234, 236, 237, 243, 244, 246, 247, 249, 250, 234, 236, 237, 243, 244, 255, 234, 259, 260, 261, 267, 271, 273, 267, 271

>Solution :

If this code is executed:

  if (!ChannelHandler_getChannelInfo(i, &tmp))
  {
     LOG_ERROR("Failed to get channel info for channel %i", mg_strLogCat, 10);
  }

szStringList[i] is never set.

All later uses of szStringList[i] might very well refer to an uninitialized value.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading