Strcpy and strcat

Hi everyone~ I am figuring out the actual use for both strcpy and strcat. I did look thru some similar topics but still can't get it clearly. Hope someone can help me on this! Thanks!

strcpy() Replace the destination with the source string.
strcat() Append the source string to the destination string.

Hi there, sry but can you provide an example? so that I can get the structure more clearer..

examples:

strcpy

strcat

void setup()
{
  char txt[25];
  
  Serial.begin(57600);

  strcpy(txt, "Hello");
  strcat(txt, " World");
  Serial.println(txt);
}

void loop()
{
}

The below has the potential to crash the application

void setup()
{
  char txt[25];
  
  Serial.begin(57600);

  strcat(txt, " World");
  Serial.println(txt);
}

void loop()
{
}

The reason is that txt is not specifically nul-terminated because it's a local variable and strcat will append at the terminating nul character; and that can basically be anywhere or nowhere in the code.

The solution is to specifically initialise the first element of the array

void setup()
{
  char txt[25];
  txt[0] = '\0';

  Serial.begin(57600);

  strcat(txt, " World");
  Serial.println(txt);
}

void loop()
{
}

Both strcpy and particularly strcat can 'overflow' the memory allocated for the result.
The 'safe' versions are strlcpy and strlcat which test/prevent overflows
See strlcpy_strlcat.ino for examples of their use.

In the above #5 examples you would get a buffer/memory overflow if you tried to add more than 24 chars to text. In that case your sketch usually just crashes, which means no debug prints to say what was wrong.
See this post for an example Use of string char causes Arduino code to restart

I also have a SafeString library that works on char[ ]s but guards against buffer overflows and prints a detailed message if you get the size of buffer wrong (no crashes)
SafeString provides safe high level methods for working with text, like trim, replace, substring etc

Alright! Thanks all~

As one would expect, there is a small code penalty for the SafeString library over the standard string processing library.:

void setup() {
  // put your setup code here, to run once:
  char result[5];
  int count;
  
  Serial.begin(9600);
  while (!Serial) ;

//  count = strlcpy(result, "Test", sizeof(result));  // Flash: 10540, data: 1516
  strcpy(result, (char *) "Test");                    //        10252        1516
  Serial.println(result);
}
void loop() {
}

Personally, I'm not in favor of runtime checks in the code. I have no good reason for feeling that way, but it makes me feel like I'm using C with training wheels.

If there's anything to be learned from the thousands of vulnerabilities and bugs in the past decades of C programming, it's that most programmers do in fact need such training wheels :grinning_face_with_smiling_eyes:

And therein is the problem: Some do...some don't need training wheels. Java, for example, forces everyone to endure runtime checks, slowing down everyone, even those who don't need the training wheels. It's a heads I win, tails you lose situation.

I don't believe this is true. To err is human, even the most experienced programmers make dumb mistakes. Granted, some fewer than others, and in some applications it's less critical, but you simply cannot expect human programmers to write bug-free software, which is why code analysis tools and runtime checks exist.

strlcat() IS party of the standard string processing library, not part of drmpf's SafeString library...

(gee, what board was THAT compiled for? I get about 50 bytes different for a Zero, 30bytes different for an Uno...)

Teensy 4.1.

I think the emphasis for me is on the word "need". If I was writing mission-critical software, I would use the tools that help to make the code more robust and bulletproof. Fortunately, I'm retired and I'm writing code I enjoy writing, primarily for personal use in my hobby: amateur radio. I don't "need" the runtime checks and I appreciate that C doesn't force me to carry baggage I don't need. Likewise, it is also fortunate that those who do need such things can use the available tools to fulfill their needs. I think my statement is true: Some do need it, some don't.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.