Hrishikesh Bawane
Hrishikesh Bawane

Reputation: 189

C++ coverity issue STRING_OVERFLOW

I see a coverity issue for the following code:

#define LEN 32
typedef char BUFFER[LEN+1];

void func(char* str)
{
    BUFFER buf;
    strcpy((char*)buf, str);
}

The issue says - "Copy into fixed size buffer (STRING_OVERFLOW)" for the strcpy() line.

I see that since str is char* its length cannot be predicted (although as per my code it will always be 32). So I tried to use strncpy() and set the null terminating character. Something like this:

strncpy((char*)buf, str, LEN);
buf[LEN] = '\0';

But this does not resolve the issue. Am I understanding the error right? Or is there something extra to be done?

Thanks in advance.

Upvotes: 2

Views: 695

Answers (1)

James Risner
James Risner

Reputation: 6076

strcpy((char*)buf, str);

The cast to char * is unneeded.

Try strcpy(buf, str);

Upvotes: 0

Related Questions