diff mbox series

[net-next,1/3] net: sysctl: remove always-true condition

Message ID 20241017152422.487406-2-atenart@kernel.org (mailing list archive)
State Accepted
Commit d631094e4d20d136f159c6e0f723b7aecbc12d2f
Delegated to: Netdev Maintainers
Headers show
Series net: sysctl: allow dump_cpumask to handle higher numbers of CPUs | expand

Checks

Context Check Description
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for net-next
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 5 this patch: 5
netdev/build_tools success No tools touched, skip
netdev/cc_maintainers warning 3 maintainers not CCed: soheil@google.com dsahern@kernel.org teknoraver@meta.com
netdev/build_clang success Errors and warnings before: 3 this patch: 3
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 4 this patch: 4
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 12 lines checked
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
netdev/contest success net-next-2024-10-19--00-00 (tests: 777)

Commit Message

Antoine Tenart Oct. 17, 2024, 3:24 p.m. UTC
Before adding a new line at the end of the temporary buffer in
dump_cpumask, a length check is performed to ensure there is space for
it.

  len = min(sizeof(kbuf) - 1, *lenp);
  len = scnprintf(kbuf, len, ...);
  if (len < *lenp)
          kbuf[len++] = '\n';

Note that the check is currently logically wrong, the written length is
compared against the output buffer, not the temporary one. However this
has no consequence as this is always true, even if fixed: scnprintf
includes a null char at the end of the buffer but the returned length do
not include it and there is always space for overriding it with a
newline.

Remove the condition.

Signed-off-by: Antoine Tenart <atenart@kernel.org>
---
 net/core/sysctl_net_core.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

Comments

Simon Horman Oct. 18, 2024, 2:05 p.m. UTC | #1
On Thu, Oct 17, 2024 at 05:24:17PM +0200, Antoine Tenart wrote:
> Before adding a new line at the end of the temporary buffer in
> dump_cpumask, a length check is performed to ensure there is space for
> it.
> 
>   len = min(sizeof(kbuf) - 1, *lenp);
>   len = scnprintf(kbuf, len, ...);
>   if (len < *lenp)
>           kbuf[len++] = '\n';
> 
> Note that the check is currently logically wrong, the written length is
> compared against the output buffer, not the temporary one. However this
> has no consequence as this is always true, even if fixed: scnprintf
> includes a null char at the end of the buffer but the returned length do
> not include it and there is always space for overriding it with a
> newline.
> 
> Remove the condition.
> 
> Signed-off-by: Antoine Tenart <atenart@kernel.org>

Thanks for separating out this and patch 2/3.
It makes it much easier to reason with these changes.

Reviewed-by: Simon Horman <horms@kernel.org>
diff mbox series

Patch

diff --git a/net/core/sysctl_net_core.c b/net/core/sysctl_net_core.c
index b60fac380cec..e7c0121dfaa1 100644
--- a/net/core/sysctl_net_core.c
+++ b/net/core/sysctl_net_core.c
@@ -69,8 +69,10 @@  static void dump_cpumask(void *buffer, size_t *lenp, loff_t *ppos,
 		return;
 	}
 
-	if (len < *lenp)
-		kbuf[len++] = '\n';
+	/* scnprintf writes a trailing null char not counted in the returned
+	 * length, override it with a newline.
+	 */
+	kbuf[len++] = '\n';
 	memcpy(buffer, kbuf, len);
 	*lenp = len;
 	*ppos += len;