LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Attempt to write beyond end of string

I'm in need of some knowledge-based information I'm trying to recall from my programming days how many characters can a pointer hold? Below is where I'm getting an error with an attempt to write beyond end of string on the pointer variable product.

 

Any suggests would be great thanks!

 

int GetProductName (char devName[DAQ_DEVICE_NAME_LEN+1], char *product)
{
int error = 0;
char cInfo[4100] = {"\0"};

memset( cInfo, '\0', 4100 * sizeof(char));
if ((error = DAQmxGetDeviceAttribute (devName, DAQmx_Dev_ProductType, cInfo, 4100)) != 0) goto Error;
if (strlen(cInfo) > DAQ_PRODUCT_NAME_LEN) cInfo[DAQ_NAME_LEN-1] = '\0';
Fmt(product, "%s<%s", cInfo);

return error;
Error:
ShowDAQmxError(0, error, "GetProductNames", 0);
return error;
}

0 Kudos
Message 1 of 5
(3,738 Views)

In your case: no one knows. But if product is not NULL it points to a memory space with at least one char length.

You have to search all calls to this GetProductName() to know the smallest.

 

Changing it to char product[DAQ_DEVICE_NAME_LEN+1] just gives false sense of safety because this works without issues:

void f( char str[5] )
{
  DebugPrintf("str = %s\n", str);
}

int __stdcall WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                       LPSTR lpszCmdLine, int nCmdShow)
{
  if (InitCVIRTE (hInstance, 0, 0) == 0)
    return -1;    /* out of memory */

  f("I am longer than 5 chars!");
  
  return 0;
}

 

IMHO better, if feasible:

Add a third parameter like "size_t maxlength" and use StringCopyMax instead of Fmt.

 

-----------------------
/* Nothing past this point should fail if the code is working as intended */
0 Kudos
Message 2 of 5
(3,704 Views)

Well, I tried your suggestion and I'm still getting the string error. I've looked throughout most of the code to see if it was a global variable. It's not, 100% local to the function nothing else. Yes, it gets used again in another function but it's a local variable again.

0 Kudos
Message 3 of 5
(3,696 Views)

Hi ,

 

 Did you try to use a shipping example from DAQmx on CVI ?

 

Did you check this link: https://knowledge.ni.com/KnowledgeArticleDetails?id=kA00Z0000019S8tSAE&l=en-US ?

Message 4 of 5
(3,688 Views)

Also, this wiki could be helpful: http://wiki.freepascal.org/NI-DAQmx_and_NI-DAQmx_Base_examples  it has an example about "get the product info".

Message 5 of 5
(3,687 Views)