[GH-ISSUE #2365] Missing memory wrapper mem_realloc() #1583

Closed
opened 2026-10-15 04:20:43 +07:00 by bsakaguchi · 11 comments
Owner

Originally created by @ghost on GitHub (Dec 16, 2016).
Original GitHub issue: https://github.com/openwall/john/issues/2365

Originally assigned to: @magnumripper on GitHub.

Looking at the leaks in 'my' random tests, I noticed it is complaining about realloc().

  • there is (already) a MEMDBG_realloc() in memdbg.c,
  • only the wrapper mem_realloc() is missing in memory.c

Is it by design or a mistake?

Originally created by @ghost on GitHub (Dec 16, 2016). Original GitHub issue: https://github.com/openwall/john/issues/2365 Originally assigned to: @magnumripper on GitHub. Looking at the leaks in 'my' random tests, I noticed it is complaining about realloc(). - there is (already) a MEMDBG_realloc() in memdbg.c, - only the wrapper mem_realloc() is missing in memory.c Is it by design or a mistake?
bsakaguchi 2026-10-15 04:20:43 +07:00
Author
Owner

@ghost commented on GitHub (Dec 16, 2016):

BTW: it also complains about a leak in path.c. The problem is that unless john proper uses something like the pseudo code below, any try to 'fix' the leak is much worse than the leak itself (we have to do a lot of copy/paste).

const char * tools[] = {
	"unshadow", "unafs", "unique", NULL
};

if (find_in_list(name, tools)) {
	CPU_detect_or_fallback(argv, 0);

	if (!strcmp(name, "unshadow"))
		ret = unshadow(argc, argv);

	else if (!strcmp(name, "unafs"))
		ret = unafs(argc, argv);

	else if (!strcmp(name, "unique"))
		ret = unique(argc, argv);

	return ret;
}
<!-- gh-comment-id:267658136 --> @ghost commented on GitHub (Dec 16, 2016): BTW: it also complains about a leak in path.c. The problem is that unless john proper uses something like the pseudo code below, any try to 'fix' the leak is much worse than the leak itself (we have to do a lot of copy/paste). ``` const char * tools[] = { "unshadow", "unafs", "unique", NULL }; if (find_in_list(name, tools)) { CPU_detect_or_fallback(argv, 0); if (!strcmp(name, "unshadow")) ret = unshadow(argc, argv); else if (!strcmp(name, "unafs")) ret = unafs(argc, argv); else if (!strcmp(name, "unique")) ret = unique(argc, argv); return ret; } ```
Author
Owner

@magnumripper commented on GitHub (Dec 17, 2016):

What's the original warning/complaint in path.c? Is it some alloc we can turn into alloc_tiny?

<!-- gh-comment-id:267790578 --> @magnumripper commented on GitHub (Dec 17, 2016): What's the original warning/complaint in path.c? Is it some alloc we can turn into alloc_tiny?
Author
Owner

@ghost commented on GitHub (Dec 17, 2016):

[edited]

Short: we call path_init() without calling path_done() => ONLY when a *2john is run.

Message:

Index : alloc# :   Size : File(Line)  [first 20 bytes, or size of bytes]
1     : 1      :   4096 : path.c(101)  ../run/.............
<!-- gh-comment-id:267791004 --> @ghost commented on GitHub (Dec 17, 2016): **[edited]** Short: we call path_init() without calling path_done() => ONLY when a *2john is run. Message: ``` Index : alloc# : Size : File(Line) [first 20 bytes, or size of bytes] 1 : 1 : 4096 : path.c(101) ../run/............. ```
Author
Owner

@magnumripper commented on GitHub (Dec 18, 2016):

OK so it's a leak-once and then we exit anyway. We could fix it just to avoid seeing the complaint (we've done so with other things) but there's no other reason.

<!-- gh-comment-id:267812186 --> @magnumripper commented on GitHub (Dec 18, 2016): OK so it's a leak-once and then we exit anyway. We could fix it just to avoid seeing the complaint (we've done so with other things) but there's no other reason.
Author
Owner

@magnumripper commented on GitHub (Dec 18, 2016):

So back to the original issue. We could/should add a mem_realloc with error checking a la mem_alloc but that "miss" itself should not be the problem here.

What are the complaints you see? What is missing? Under a memdbg build, it seems to me (at a quick glance just using grep) that memdbg.h defines realloc as macro pointing to MEMDBG_realloc. BTW things like that are the reasons the #include "memdbg.h" must be the very last sourcing of headers.

<!-- gh-comment-id:267849591 --> @magnumripper commented on GitHub (Dec 18, 2016): So back to the original issue. We could/should add a mem_realloc with error checking a la mem_alloc but that "miss" itself should not be the problem here. What are the complaints you see? What is missing? Under a memdbg build, it seems to me (at a quick glance just using grep) that memdbg.h defines realloc as macro pointing to MEMDBG_realloc. BTW things like that are the reasons the `#include "memdbg.h"` must be the very last sourcing of headers.
Author
Owner

@ghost commented on GitHub (Dec 18, 2016):

Index : alloc# :   Size : File(Line)  [first 20 bytes, or size of bytes]
0     : 11     :   4096 : zip2john.c(328)  f54860d64e4a5a15c1af

Have you noticed the #if !defined(__MEMDBG__)?

<!-- gh-comment-id:267850002 --> @ghost commented on GitHub (Dec 18, 2016): ``` Index : alloc# : Size : File(Line) [first 20 bytes, or size of bytes] 0 : 11 : 4096 : zip2john.c(328) f54860d64e4a5a15c1af ``` Have you noticed the `#if !defined(__MEMDBG__)`?
Author
Owner

@magnumripper commented on GitHub (Dec 18, 2016):

Yes but __MEMDBG__ is only defined when called from memdbg.c itself (do not confuse __MEMDBG__ with eg. MEMDBG_ON)

Edit: Perhaps we should refactor it to __MEMDBG_C__

<!-- gh-comment-id:267850213 --> @magnumripper commented on GitHub (Dec 18, 2016): Yes but `__MEMDBG__` is only defined when called *from* memdbg.c itself (do not confuse `__MEMDBG__` with eg. `MEMDBG_ON`) Edit: Perhaps we should refactor it to `__MEMDBG_C__`
Author
Owner

@magnumripper commented on GitHub (Dec 18, 2016):

Ah, OK this is a normal leak-once. It not about realloc, it could have been alloc just as well. We never free Buf - we just let the OS do it for us.

<!-- gh-comment-id:267850372 --> @magnumripper commented on GitHub (Dec 18, 2016): Ah, OK this is a normal leak-once. It not about realloc, it could have been alloc just as well. We never free Buf - we just let the OS do it for us.
Author
Owner

@magnumripper commented on GitHub (Dec 18, 2016):

And that code is subject to change anyway, see https://github.com/magnumripper/JohnTheRipper/issues/2352#issuecomment-265020494

<!-- gh-comment-id:267850441 --> @magnumripper commented on GitHub (Dec 18, 2016): And that code is subject to change anyway, see https://github.com/magnumripper/JohnTheRipper/issues/2352#issuecomment-265020494
Author
Owner

@ghost commented on GitHub (Dec 18, 2016):

It was about style or consistency: if I can't use malloc(), then I can't use realloc(). Indeed, it is not a "real leak". I'm being a nuisance.

I prefer: __MEMDBG_C_FILE__

<!-- gh-comment-id:267855166 --> @ghost commented on GitHub (Dec 18, 2016): It was about style or consistency: if I can't use malloc(), then I can't use realloc(). Indeed, it is not a "real leak". I'm being a nuisance. I prefer: ```__MEMDBG_C_FILE__```
Author
Owner

@magnumripper commented on GitHub (Dec 19, 2016):

Agreed, we should add mem_realloc. In fact some of the realloc calls are much more likely to fail than any of the allocs. We have things like if (size < needed) { size *= 2; buf = realloc(buf, size); }. So ensuring proper handling of errors (which is slightly more complicated for realloc than for alloc) is a good thing.

<!-- gh-comment-id:267892800 --> @magnumripper commented on GitHub (Dec 19, 2016): Agreed, we should add mem_realloc. In fact some of the realloc calls are much more likely to fail than any of the allocs. We have things like `if (size < needed) { size *= 2; buf = realloc(buf, size); }`. So ensuring proper handling of errors (which is slightly more complicated for realloc than for alloc) is a good thing.
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#1583
No description provided.