Module Name:    src
Committed By:   rillig
Date:           Sun May 12 09:07:41 UTC 2024

Modified Files:
        src/usr.bin/xlint/lint1: cgram.y lex.c tree.c

Log Message:
lint: don't call memcpy with null pointer

Even copying 0 bytes from a null pointer invokes undefined behavior.


To generate a diff of this commit:
cvs rdiff -u -r1.502 -r1.503 src/usr.bin/xlint/lint1/cgram.y
cvs rdiff -u -r1.226 -r1.227 src/usr.bin/xlint/lint1/lex.c
cvs rdiff -u -r1.642 -r1.643 src/usr.bin/xlint/lint1/tree.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/xlint/lint1/cgram.y
diff -u src/usr.bin/xlint/lint1/cgram.y:1.502 src/usr.bin/xlint/lint1/cgram.y:1.503
--- src/usr.bin/xlint/lint1/cgram.y:1.502	Sun May 12 08:48:36 2024
+++ src/usr.bin/xlint/lint1/cgram.y	Sun May 12 09:07:41 2024
@@ -1,5 +1,5 @@
 %{
-/* $NetBSD: cgram.y,v 1.502 2024/05/12 08:48:36 rillig Exp $ */
+/* $NetBSD: cgram.y,v 1.503 2024/05/12 09:07:41 rillig Exp $ */
 
 /*
  * Copyright (c) 1996 Christopher G. Demetriou.  All Rights Reserved.
@@ -35,7 +35,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID)
-__RCSID("$NetBSD: cgram.y,v 1.502 2024/05/12 08:48:36 rillig Exp $");
+__RCSID("$NetBSD: cgram.y,v 1.503 2024/05/12 09:07:41 rillig Exp $");
 #endif
 
 #include <limits.h>
@@ -118,9 +118,10 @@ attribute_list_add(attribute_list *list,
 		attribute *old_attrs = list->attrs;
 		list->cap = 16 + 2 * list->cap;
 		list->attrs = block_zero_alloc(
-		    list->cap * sizeof(*list->attrs), "attribute_list.attrs");
-		memcpy(list->attrs, old_attrs,
-		    list->len * sizeof(*list->attrs));
+		    list->cap * sizeof(*list->attrs), "attribute[]");
+		if (list->len > 0)
+			memcpy(list->attrs, old_attrs,
+			    list->len * sizeof(*list->attrs));
 	}
 	list->attrs[list->len++] = attr;
 }

Index: src/usr.bin/xlint/lint1/lex.c
diff -u src/usr.bin/xlint/lint1/lex.c:1.226 src/usr.bin/xlint/lint1/lex.c:1.227
--- src/usr.bin/xlint/lint1/lex.c:1.226	Sun May 12 08:48:36 2024
+++ src/usr.bin/xlint/lint1/lex.c	Sun May 12 09:07:41 2024
@@ -1,4 +1,4 @@
-/* $NetBSD: lex.c,v 1.226 2024/05/12 08:48:36 rillig Exp $ */
+/* $NetBSD: lex.c,v 1.227 2024/05/12 09:07:41 rillig Exp $ */
 
 /*
  * Copyright (c) 1996 Christopher G. Demetriou.  All Rights Reserved.
@@ -38,7 +38,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID)
-__RCSID("$NetBSD: lex.c,v 1.226 2024/05/12 08:48:36 rillig Exp $");
+__RCSID("$NetBSD: lex.c,v 1.227 2024/05/12 09:07:41 rillig Exp $");
 #endif
 
 #include <ctype.h>
@@ -1545,9 +1545,10 @@ seq_reserve(balanced_token_sequence *seq
 		seq->cap = 16 + 2 * seq->cap;
 		const balanced_token *old_tokens = seq->tokens;
 		balanced_token *new_tokens = block_zero_alloc(
-		    seq->cap * sizeof(*seq->tokens), "balanced_tokens");
-		memcpy(new_tokens, old_tokens,
-		    seq->len * sizeof(*seq->tokens));
+		    seq->cap * sizeof(*seq->tokens), "balanced_token[]");
+		if (seq->len > 0)
+			memcpy(new_tokens, old_tokens,
+			    seq->len * sizeof(*seq->tokens));
 		seq->tokens = new_tokens;
 	}
 }

Index: src/usr.bin/xlint/lint1/tree.c
diff -u src/usr.bin/xlint/lint1/tree.c:1.642 src/usr.bin/xlint/lint1/tree.c:1.643
--- src/usr.bin/xlint/lint1/tree.c:1.642	Sat May 11 15:53:38 2024
+++ src/usr.bin/xlint/lint1/tree.c	Sun May 12 09:07:41 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: tree.c,v 1.642 2024/05/11 15:53:38 rillig Exp $	*/
+/*	$NetBSD: tree.c,v 1.643 2024/05/12 09:07:41 rillig Exp $	*/
 
 /*
  * Copyright (c) 1994, 1995 Jochen Pohl
@@ -37,7 +37,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID)
-__RCSID("$NetBSD: tree.c,v 1.642 2024/05/11 15:53:38 rillig Exp $");
+__RCSID("$NetBSD: tree.c,v 1.643 2024/05/12 09:07:41 rillig Exp $");
 #endif
 
 #include <float.h>
@@ -4214,10 +4214,10 @@ add_function_argument(function_call *cal
 	if (call->args_len >= call->args_cap) {
 		call->args_cap += 8;
 		tnode_t **new_args = expr_zero_alloc(
-		    call->args_cap * sizeof(*call->args),
-		    "function_call.args");
-		memcpy(new_args, call->args,
-		    call->args_len * sizeof(*call->args));
+		    call->args_cap * sizeof(*call->args), "tnode*[]");
+		if (call->args_len > 0)
+			memcpy(new_args, call->args,
+			    call->args_len * sizeof(*call->args));
 		call->args = new_args;
 	}
 	call->args[call->args_len++] = arg;

Reply via email to