Need Code Translation From Pascal/Delph to C++

Started by
10 comments, last by Vortez 10 years, 6 months ago

If you don't want strcpy everywhere, just write a function for that.

void FillCustomer(char *pName, char *pCell, char *pCity)
{
strncpy(customer.Name, pName, 25);
strncpy(customer.City, pName, 12);
strncpy(customer.Cell, pName, 20);
}

Using strncpy() like that doesn't fix the buffer overflow problem.

You need to do something like this or there will be no null terminator when the string is too long:


void FillCustomer(char *pName, char *pCell, char *pCity)
{
  strncpy(customer.Name, pName, 25); customer.Name[25 - 1] = 0;
  strncpy(customer.City, pName, 12); customer.City[12 - 1] = 0;
  strncpy(customer.Cell, pName, 20); customer.Cell[20 - 1] = 0;
}
Advertisement

Not if you ZeroMemory() the structure beforehand, like it should be done. Or


void FillCustomer(char *pName, char *pCell, char *pCity)
{
  ZeroMemory(&customer, sizeof(customer));
  strncpy(customer.Name, pName, 25); 
  strncpy(customer.City, pName, 12);
  strncpy(customer.Cell, pName, 20);
}

This topic is closed to new replies.

Advertisement