Welcome! Log In Create A New Profile

Advanced

Re: Core: Avoid memcpy from NULL

Ben Kallus
January 03, 2024 07:00PM
> Still, general style guidelines suggests that the code shouldn't
> be written this way, and the only reason for j++ in the line in
> question is that it mimics corresponding IPv4 code.

> It's not "just happens".

The point I'm trying to make is that ensuring correctness with
function-like macros is difficult, both because of operator precedence
and argument reevaluation. Expecting contributors to read the
definitions of every macro they use becomes more and more cumbersome
as the codebase expands, especially when some symbols are variably
macros or functions depending on the state of (even infrequently-used)
compile-time constants.

All that said, upon further reflection, I think the UB issue is best
solved outside of ngx_strcpy, where the overhead of an extra check may
have a performance impact. The following patch is sufficient to
silence UBSan in my configuration:

# HG changeset patch
# User Ben Kallus <benjamin.p.kallus.gr@dartmouth.edu>
# Date 1704322684 18000
# Wed Jan 03 17:58:04 2024 -0500
# Node ID 04eb4b1622d1a488f14bb6d5af25e422ff23d82d
# Parent ee40e2b1d0833b46128a357fbc84c6e23be9be07
Add check to ngx_pstrdup to prevent 0-length memcpy.

diff -r ee40e2b1d083 -r 04eb4b1622d1 src/core/ngx_string.c
--- a/src/core/ngx_string.c Mon Dec 25 21:15:48 2023 +0400
+++ b/src/core/ngx_string.c Wed Jan 03 17:58:04 2024 -0500
@@ -77,8 +77,8 @@
u_char *dst;

dst = ngx_pnalloc(pool, src->len);
- if (dst == NULL) {
- return NULL;
+ if (dst == NULL || src->len == 0) {
+ return dst;
}

ngx_memcpy(dst, src->data, src->len);
_______________________________________________
nginx-devel mailing list
nginx-devel@nginx.org
https://mailman.nginx.org/mailman/listinfo/nginx-devel
Subject Author Views Posted

Core: Avoid memcpy from NULL

Ben Kallus 689 December 13, 2023 11:10AM

Re: Core: Avoid memcpy from NULL

Maxim Dounin 154 December 14, 2023 09:44PM

Re: Core: Avoid memcpy from NULL

Dipl. Ing. Sergey Brester via nginx-devel 150 December 15, 2023 09:48AM

Re: Core: Avoid memcpy from NULL

Ben Kallus 123 December 15, 2023 11:30AM

Re: Core: Avoid memcpy from NULL

Maxim Dounin 138 December 15, 2023 11:18PM

Re: Core: Avoid memcpy from NULL

Ben Kallus 154 December 16, 2023 04:28PM

Re: Core: Avoid memcpy from NULL

Maxim Dounin 149 December 20, 2023 07:36PM

Re: Core: Avoid memcpy from NULL

Ben Kallus 125 December 29, 2023 11:52AM

Re: Core: Avoid memcpy from NULL

Maxim Dounin 151 December 31, 2023 02:54PM

Re: Core: Avoid memcpy from NULL

Ben Kallus 136 January 03, 2024 07:00PM

Re: Core: Avoid memcpy from NULL

Maxim Dounin 141 January 04, 2024 11:12AM

Re: Core: Avoid memcpy from NULL

Ben Kallus 135 January 09, 2024 11:20AM

Re: Core: Avoid memcpy from NULL

Maxim Dounin 119 January 09, 2024 02:26PM

Re: Core: Avoid memcpy from NULL

Ben Kallus 143 January 23, 2024 07:10PM

Re: Core: Avoid memcpy from NULL

Maxim Dounin 117 January 24, 2024 03:58AM

Re: Core: Avoid memcpy from NULL

karton 131 January 24, 2024 10:54PM



Sorry, you do not have permission to post/reply in this forum.

Online Users

Guests: 78
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 500 on July 15, 2024
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready