[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <c6452618-0161-eefc-0730-b10eac823228@rasmusvillemoes.dk>
Date: Wed, 30 Oct 2019 15:59:58 +0100
From: Rasmus Villemoes <linux@...musvillemoes.dk>
To: Liu Xiang <liuxiang_1999@....com>, linux-kernel@...r.kernel.org
Subject: Re: [PATCH] lib: string: reduce unnecessary loop in strncpy
On 30/10/2019 15.14, Liu Xiang wrote:
> Now in strncpy, even src[0] is 0, loop will execute count times until
> count is 0. It is better to exit the loop immediately when *src is 0.
Please read "man strncpy".
There's a reason the loop is written in that somewhat convoluted way:
The behavior of strncpy is mandated by the C standard, and if the src
string is shorter than the destination buffer, the rest must be
0-filled. So if we hit a nul byte before running out of count, we keep
copying that nul byte to the rest of the destination.
Rasmus
Powered by blists - more mailing lists