Re: [hackers] [dwm][PATCH] ensure layout symbol is properly nul-terminated

From: NRK <>
Date: Sun, 14 Aug 2022 04:33:03 +0600

On Sat, Aug 13, 2022 at 08:47:41AM +0200, Stein Gunnar Bakkeby wrote:
> Would it be easier to make the layout
> symbol size 15 (one less than the monitor symbol)?

Not really, since the src might still end up not containing the
nul-byte, the 16th byte will have to nul-terminated anyways. For

        char src[4] = "abcd"; /* no nul-byte in here */
        char dst[5];
        // strncpy
        strncpy(dst, src, sizeof dst - 1); /* only copied 4 bytes. the 5th byte
                                              in dst needs to nul-terminated */
        dst[sizeof dst - 1] = 0;
        // or using stpncpy
        *stpncpy(dst, src, sizeof dst - 1) = '\0';

Or if you can ensure the last byte in dst is set to nul at the
beginning, and is never written to again in the entire program then you
can probably skip the manual nul-termination.

But IMO hunting down all instances where `dst` is modified is more
effort and error-prone than just nul-terminating right at the call site.

> Doesn't this affect readability?

I find it more readable to have the nul-termination and copy done in a
single statement. But readability is subjective, so there's no
definitive answer to that.

Received on Sun Aug 14 2022 - 00:33:03 CEST

This archive was generated by hypermail 2.3.0 : Sun Aug 14 2022 - 00:36:34 CEST