[GH-ISSUE #2233] 2 -Wsizeof-pointer-memaccess clang warnings in --enable-memdbg debug build #1493

Closed
opened 2026-10-15 04:18:39 +07:00 by bsakaguchi · 9 comments
Owner

Originally created by @frank-dittrich on GitHub (Aug 24, 2016).
Original GitHub issue: https://github.com/openwall/john/issues/2233

memdbg.c:588:17: warning: argument to 'sizeof' in 'memcpy' call is the same pointer type 'MEMDBG_HDR *' (aka 'struct _hdr *') as the destination; expected 'MEMDBG_HDR' (aka 'struct _hdr')
      or an explicit length [-Wsizeof-pointer-memaccess]
        memcpy(p2, &p, sizeof(p));
               ~~      ^~~~~~~~~
memdbg.c:636:17: warning: argument to 'sizeof' in 'memcpy' call is the same pointer type 'MEMDBG_HDR *' (aka 'struct _hdr *') as the destination; expected 'MEMDBG_HDR' (aka 'struct _hdr')
      or an explicit length [-Wsizeof-pointer-memaccess]
        memcpy(p2, &p, sizeof(p));
               ~~      ^~~~~~~~~
2 warnings generated.
Originally created by @frank-dittrich on GitHub (Aug 24, 2016). Original GitHub issue: https://github.com/openwall/john/issues/2233 ``` memdbg.c:588:17: warning: argument to 'sizeof' in 'memcpy' call is the same pointer type 'MEMDBG_HDR *' (aka 'struct _hdr *') as the destination; expected 'MEMDBG_HDR' (aka 'struct _hdr') or an explicit length [-Wsizeof-pointer-memaccess] memcpy(p2, &p, sizeof(p)); ~~ ^~~~~~~~~ memdbg.c:636:17: warning: argument to 'sizeof' in 'memcpy' call is the same pointer type 'MEMDBG_HDR *' (aka 'struct _hdr *') as the destination; expected 'MEMDBG_HDR' (aka 'struct _hdr') or an explicit length [-Wsizeof-pointer-memaccess] memcpy(p2, &p, sizeof(p)); ~~ ^~~~~~~~~ 2 warnings generated. ```
bsakaguchi 2026-10-15 04:18:39 +07:00
Author
Owner

@jfoug commented on GitHub (Aug 24, 2016):

Stupid ass warning. Just because the compiler writer 'expects' something does not mean crap!

Hopefully an easy fix however. Just put MEMDBG_HDR * into the sizeof, vs the variable name itself.

<!-- gh-comment-id:242183024 --> @jfoug commented on GitHub (Aug 24, 2016): Stupid ass warning. Just because the compiler writer 'expects' something does not mean crap! Hopefully an easy fix however. Just put MEMDBG_HDR \* into the sizeof, vs the variable name itself.
Author
Owner

@jfoug commented on GitHub (Aug 24, 2016):

If the compiler is going to make wild ass guesses about behavior like this, then it should be smart enough to see that we are not copying what p points to, but copying the ADDRESS of the pointer p. Oh well.

<!-- gh-comment-id:242184110 --> @jfoug commented on GitHub (Aug 24, 2016): If the compiler is going to make wild ass guesses about behavior like this, then it should be smart enough to see that we are not copying what p points to, but copying the ADDRESS of the pointer p. Oh well.
Author
Owner

@jfoug commented on GitHub (Aug 24, 2016):

Ok, I have clang installed on my fedora22, and it is coring, so I can now hopefully better debug this damn issue.

<!-- gh-comment-id:242185368 --> @jfoug commented on GitHub (Aug 24, 2016): Ok, I have clang installed on my fedora22, and it is coring, so I can now hopefully better debug this damn issue.
Author
Owner

@frank-dittrich commented on GitHub (Aug 24, 2016):

No luck.

memdbg.c:588:17: warning: argument to 'sizeof' in 'memcpy' call is the same pointer type 'MEMDBG_HDR *' (aka 'struct _hdr *') as the destination; expected 'MEMDBG_HDR' (aka 'struct _hdr')
      or an explicit length [-Wsizeof-pointer-memaccess]
        memcpy(p2, &p, sizeof(MEMDBG_HDR *));
               ~~      ^~~~~~~~~~~~~~~~~~~~
memdbg.c:636:17: warning: argument to 'sizeof' in 'memcpy' call is the same pointer type 'MEMDBG_HDR *' (aka 'struct _hdr *') as the destination; expected 'MEMDBG_HDR' (aka 'struct _hdr')
      or an explicit length [-Wsizeof-pointer-memaccess]
        memcpy(p2, &p, sizeof(MEMDBG_HDR *));
               ~~      ^~~~~~~~~~~~~~~~~~~~
2 warnings generated.

File a clang bug? gcc also has that warning in -Wall, but is smart enough not to warn here.

<!-- gh-comment-id:242186411 --> @frank-dittrich commented on GitHub (Aug 24, 2016): No luck. ``` memdbg.c:588:17: warning: argument to 'sizeof' in 'memcpy' call is the same pointer type 'MEMDBG_HDR *' (aka 'struct _hdr *') as the destination; expected 'MEMDBG_HDR' (aka 'struct _hdr') or an explicit length [-Wsizeof-pointer-memaccess] memcpy(p2, &p, sizeof(MEMDBG_HDR *)); ~~ ^~~~~~~~~~~~~~~~~~~~ memdbg.c:636:17: warning: argument to 'sizeof' in 'memcpy' call is the same pointer type 'MEMDBG_HDR *' (aka 'struct _hdr *') as the destination; expected 'MEMDBG_HDR' (aka 'struct _hdr') or an explicit length [-Wsizeof-pointer-memaccess] memcpy(p2, &p, sizeof(MEMDBG_HDR *)); ~~ ^~~~~~~~~~~~~~~~~~~~ 2 warnings generated. ``` File a clang bug? gcc also has that warning in -Wall, but is smart enough not to warn here.
Author
Owner

@jfoug commented on GitHub (Aug 24, 2016):

This is 100% a clang bug. The code is (and was) correct. What it is doing is putting a the 'location' of a pointer into a known spot in memory, since the pointer distance from the front of the allocated block can vary. We have to work 'backwards' from the pointer the program has, to be able to find the REAL memory block that was allocated, since call the 'program' has is a pointer to some memory within that allocated buffer.

This was not a bug, even though clang 'thinks' it is strange behavior. I was hopeful that changing to the actual 'type' within the sizeof would have muted it, but I guess not.

<!-- gh-comment-id:242190547 --> @jfoug commented on GitHub (Aug 24, 2016): This is 100% a clang bug. The code is (and was) correct. What it is doing is putting a the 'location' of a pointer into a known spot in memory, since the pointer distance from the front of the allocated block can vary. We have to work 'backwards' from the pointer the program has, to be able to find the REAL memory block that was allocated, since call the 'program' has is a pointer to some memory within that allocated buffer. This was not a bug, even though clang 'thinks' it is strange behavior. I was hopeful that changing to the actual 'type' within the sizeof would have muted it, but I guess not.
Author
Owner

@jfoug commented on GitHub (Aug 24, 2016):

I am not pushing any change, if there was not muting of the warning.

<!-- gh-comment-id:242212426 --> @jfoug commented on GitHub (Aug 24, 2016): I am not pushing any change, if there was not muting of the warning.
Author
Owner

@kholia commented on GitHub (Mar 8, 2017):

@magnumripper Should we close bugs (like this one for example) which need no further action or where there is no further step that can be taken?

<!-- gh-comment-id:285004482 --> @kholia commented on GitHub (Mar 8, 2017): @magnumripper Should we close bugs (like this one for example) which need no further action or where there is no further step that can be taken?
Author
Owner

@magnumripper commented on GitHub (Mar 8, 2017):

Yeah I think so.

<!-- gh-comment-id:285039171 --> @magnumripper commented on GitHub (Mar 8, 2017): Yeah I think so.
Author
Owner

@ghost commented on GitHub (Apr 24, 2017):

Isn't this the "fix"? I mean, now the destination pointer is only a pointer; a 'generic' one?

From 2892057c0797eb2cdfa751cdc2e0ccf96a4c5025 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Claudio=20Andr=C3=A9?= <claudioandre.br@gmail.com>
Date: Thu, 20 Apr 2017 22:36:12 -0300
Subject: [PATCH 2/2] maintenance: fix the expected data type size

---
 src/memdbg.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/src/memdbg.c b/src/memdbg.c
index f7551f5e1..2cadca801 100644
--- a/src/memdbg.c
+++ b/src/memdbg.c
@@ -545,7 +545,8 @@ void * MEMDBG_calloc(size_t count, size_t size, char *file, int line)
  */
 void * MEMDBG_alloc(size_t size, char *file, int line)
 {
-	MEMDBG_HDR      *p, *p2;
+	MEMDBG_HDR      *p;
+	void *p2;
 
 	if ( ((signed long long)mem_size) < 0)
 		fprintf(stderr, "MEMDBG_alloc "LLd" %s:%d  mem:"LLd"\n", (unsigned long long)size, file, line, (unsigned long long)mem_size);
@@ -614,7 +615,8 @@ void * MEMDBG_alloc(size_t size, char *file, int line)
  */
 void * MEMDBG_alloc_align(size_t size, int align, char *file, int line)
 {
-	MEMDBG_HDR      *p, *p2;
+	MEMDBG_HDR      *p;
+	void *p2;
 	char *p3;
 
 	if ( ((signed long long)mem_size) < 0)
-- 
2.11.0

I had to apply this to CI (and all is ok) .

<!-- gh-comment-id:296507816 --> @ghost commented on GitHub (Apr 24, 2017): Isn't this the "fix"? I mean, now the destination pointer is only a pointer; a 'generic' one? ``` From 2892057c0797eb2cdfa751cdc2e0ccf96a4c5025 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Claudio=20Andr=C3=A9?= <claudioandre.br@gmail.com> Date: Thu, 20 Apr 2017 22:36:12 -0300 Subject: [PATCH 2/2] maintenance: fix the expected data type size --- src/memdbg.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/memdbg.c b/src/memdbg.c index f7551f5e1..2cadca801 100644 --- a/src/memdbg.c +++ b/src/memdbg.c @@ -545,7 +545,8 @@ void * MEMDBG_calloc(size_t count, size_t size, char *file, int line) */ void * MEMDBG_alloc(size_t size, char *file, int line) { - MEMDBG_HDR *p, *p2; + MEMDBG_HDR *p; + void *p2; if ( ((signed long long)mem_size) < 0) fprintf(stderr, "MEMDBG_alloc "LLd" %s:%d mem:"LLd"\n", (unsigned long long)size, file, line, (unsigned long long)mem_size); @@ -614,7 +615,8 @@ void * MEMDBG_alloc(size_t size, char *file, int line) */ void * MEMDBG_alloc_align(size_t size, int align, char *file, int line) { - MEMDBG_HDR *p, *p2; + MEMDBG_HDR *p; + void *p2; char *p3; if ( ((signed long long)mem_size) < 0) -- 2.11.0 ``` ***** I had to apply this to CI (and all is ok) .
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
openwall/john#1493
No description provided.