xserver icon indicating copy to clipboard operation
xserver copied to clipboard

Xtranssock.c: fix format truncation warning

Open clhin opened this issue 1 month ago • 6 comments

I was tired of staring at this every compile, since we check if the size is appropriate before we write, we can use asprintf and the compiler now trusts that we know what we are doing, and if we didn't we returned with -1 before it could allocate.

clhin avatar Nov 14 '25 22:11 clhin

Not sure if this is a good thing, asprintf will allocate some heap memory uselessly here, where the snprintf wouldn't.

Also your code is probably wrong:

strncpy(path, buf, sizeof(s.sun_path-1));
// should be:
strncpy(path, buf, sizeof(s.sun_path) - 1);

And sizeof(s.sun_path) - 1 was already assigned to the variable maxlen a little bit higher in the function, so no need to recalculate it:

strncpy(path, buf, maxlen);

WladimirBec avatar Nov 14 '25 23:11 WladimirBec

Not sure if this is a good thing, asprintf will allocate some heap memory uselessly here, where the snprintf wouldn't.

This is unfortunately true, but I am not sure there is a better way of convincing the compiler we know what we are doing here other than simply silencing the warning.

Also your code is probably wrong:

strncpy(path, buf, sizeof(s.sun_path-1));
// should be:
strncpy(path, buf, sizeof(s.sun_path) - 1);

Fixed, thank you. The commit now passes all the cases and is ready for review.

And sizeof(s.sun_path) - 1 was already assigned to the variable maxlen a little bit higher in the function, so no need to recalculate it:

strncpy(path, buf, maxlen);

The compiler can optimize this into a constant instead of having to access memory which may take a number of cpu cycles, unless you make it constant.

clhin avatar Nov 15 '25 00:11 clhin

Not sure if this is a good thing, asprintf will allocate some heap memory uselessly here, where the snprintf wouldn't.

I could try using strcat and then strcopy to get around this, but we will still need to make a buffer on the stack that has the same lifetime as the asprintf buffer...

clhin avatar Nov 16 '25 01:11 clhin

If there are no further issues with this commit I would like it merged so that we can start thinking about setting -Wextra or -Werror

clhin avatar Nov 20 '25 00:11 clhin

If there are no further issues with this commit I would like it merged so that we can start thinking about setting -Wextra or -Werror

Don't try to set -Wextra on the X server, there are lot's of warnings there that don't indicate any issue, and fixing those would just waste a lot of time while making the code unnatural and harder to read.

About -Werror, I'm planning to set it in CI after the openssl deprecation warnings get fixed.

stefan11111 avatar Nov 20 '25 07:11 stefan11111

I tested using strcats as well and they sucessfully compile without the warning, using 3 strcats, one strncpy, and one 108 byte buffer.

clhin avatar Nov 22 '25 05:11 clhin