libqrencode icon indicating copy to clipboard operation
libqrencode copied to clipboard

qrenc: silence gcc -Wstringop-overflow warning

Open sgn opened this issue 6 years ago • 5 comments

Increase the size passed to strncat(3) by 1 to silence gcc warning. The increased value is accounted for the NULL terminator.

This change won't change anything at runtime since strncat(3) will append that '\0' if size of dst is reached but '\0' is not found.

sgn avatar Nov 25 '19 13:11 sgn

Thank you @sgn.

According to this stackoverflow, we could use strcat() instead of strncat(). https://stackoverflow.com/questions/53408543/strncat-wformat-overflow-warning-when-using-gcc-8-2-1

Actually, we just use static strings as the src of strncat() and in these case there's no need to calculate the length of the src strings by hand.

Can I have your thoughts?

fukuchi avatar Dec 12 '19 05:12 fukuchi

On 2019-12-11 21:04:58-0800, Kentaro Fukuchi [email protected] wrote:

Actually, we just use static strings as the src of strncat() and in these case there's no need to calculate the length of the src strings by hand.

Yes, we could use strcat(3) instead of strncat(3) in this case. And, it could be simpler to use strcat(3) instead.

After a harder thought, I think strcat(3) isn't really optimal in this case.

With strcat(3), we're repeatedly traverse through buffer.

I think it's better to use memcpy instead.

I could prepare the patch myself if you think it's a good idea.

-- Danh

sgn avatar Dec 12 '19 12:12 sgn

Hi @fukuchi,

I've pushed a series of change to replace strncat with strcpy.

The last commit is intended for not altering options in writeANSI and writeASCII, if you don't like the last patch, feel free to drop it.

sgn avatar Dec 12 '19 13:12 sgn

Hi, I just got these warnings and found this request. Why do you use strncat()? To avoid buffer overflows, then keep it but change the size parameter (have a look at https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83404). If you're really sure the buffer is big enough, then just use strcat(). Same with strncpy(). Cheers, André

Woebbeking avatar Jan 21 '21 17:01 Woebbeking

I think the buffer is always large enough. Let's just use strcpy(3).

sgn avatar Jan 22 '21 00:01 sgn