From 894358a8f3e338b339b6c37233edef794b312087 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Tue, 7 Mar 2006 06:31:04 +0000 Subject: r13915: Fixed a very interesting class of realloc() bugs found by Coverity. realloc can return NULL in one of two cases - (1) the realloc failed, (2) realloc succeeded but the new size requested was zero, in which case this is identical to a free() call. The error paths dealing with these two cases should be different, but mostly weren't. Secondly the standard idiom for dealing with realloc when you know the new size is non-zero is the following : tmp = realloc(p, size); if (!tmp) { SAFE_FREE(p); return error; } else { p = tmp; } However, there were *many* *many* places in Samba where we were using the old (broken) idiom of : p = realloc(p, size) if (!p) { return error; } which will leak the memory pointed to by p on realloc fail. This commit (hopefully) fixes all these cases by moving to a standard idiom of : p = SMB_REALLOC(p, size) if (!p) { return error; } Where if the realloc returns null due to the realloc failing or size == 0 we *guarentee* that the storage pointed to by p has been freed. This allows me to remove a lot of code that was dealing with the standard (more verbose) method that required a tmp pointer. This is almost always what you want. When a realloc fails you never usually want the old memory, you want to free it and get into your error processing asap. For the 11 remaining cases where we really do need to keep the old pointer I have invented the new macro SMB_REALLOC_KEEP_OLD_ON_ERROR, which can be used as follows : tmp = SMB_REALLOC_KEEP_OLD_ON_ERROR(p, size); if (!tmp) { SAFE_FREE(p); return error; } else { p = tmp; } SMB_REALLOC_KEEP_OLD_ON_ERROR guarentees never to free the pointer p, even on size == 0 or realloc fail. All this is done by a hidden extra argument to Realloc(), BOOL free_old_on_error which is set appropriately by the SMB_REALLOC and SMB_REALLOC_KEEP_OLD_ON_ERROR macros (and their array counterparts). It remains to be seen what this will do to our Coverity bug count :-). Jeremy. (This used to be commit 1d710d06a214f3f1740e80e0bffd6aab44aac2b0) --- source3/client/client.c | 6 ++---- source3/client/clitar.c | 7 ++----- source3/client/smbctool.c | 6 ++---- 3 files changed, 6 insertions(+), 13 deletions(-) (limited to 'source3/client') diff --git a/source3/client/client.c b/source3/client/client.c index 403074b22b..0126e17c5b 100644 --- a/source3/client/client.c +++ b/source3/client/client.c @@ -463,19 +463,17 @@ static void adjust_do_list_queue(void) static void add_to_do_list_queue(const char* entry) { - char *dlq; long new_end = do_list_queue_end + ((long)strlen(entry)) + 1; while (new_end > do_list_queue_size) { do_list_queue_size *= 2; DEBUG(4,("enlarging do_list_queue to %d\n", (int)do_list_queue_size)); - dlq = SMB_REALLOC(do_list_queue, do_list_queue_size); - if (! dlq) { + do_list_queue = SMB_REALLOC(do_list_queue, do_list_queue_size); + if (! do_list_queue) { d_printf("failure enlarging do_list_queue to %d bytes\n", (int)do_list_queue_size); reset_do_list_queue(); } else { - do_list_queue = dlq; memset(do_list_queue + do_list_queue_size / 2, 0, do_list_queue_size / 2); } diff --git a/source3/client/clitar.c b/source3/client/clitar.c index cd0ce27eb5..ff9bc1f0fb 100644 --- a/source3/client/clitar.c +++ b/source3/client/clitar.c @@ -1510,16 +1510,13 @@ static int read_inclusion_file(char *filename) } if ((strlen(buf) + 1 + inclusion_buffer_sofar) >= inclusion_buffer_size) { - char *ib; inclusion_buffer_size *= 2; - ib = SMB_REALLOC(inclusion_buffer,inclusion_buffer_size); - if (! ib) { + inclusion_buffer = SMB_REALLOC(inclusion_buffer,inclusion_buffer_size); + if (!inclusion_buffer) { DEBUG(0,("failure enlarging inclusion buffer to %d bytes\n", inclusion_buffer_size)); error = 1; break; - } else { - inclusion_buffer = ib; } } diff --git a/source3/client/smbctool.c b/source3/client/smbctool.c index c368f7a193..6c89f5ea82 100644 --- a/source3/client/smbctool.c +++ b/source3/client/smbctool.c @@ -544,19 +544,17 @@ static void adjust_do_list_queue(void) static void add_to_do_list_queue(const char* entry) { - char *dlq; long new_end = do_list_queue_end + ((long)strlen(entry)) + 1; while (new_end > do_list_queue_size) { do_list_queue_size *= 2; DEBUG(4,("enlarging do_list_queue to %d\n", (int)do_list_queue_size)); - dlq = SMB_REALLOC(do_list_queue, do_list_queue_size); - if (! dlq) { + do_list_queue = SMB_REALLOC(do_list_queue, do_list_queue_size); + if (!do_list_queue) { d_printf("failure enlarging do_list_queue to %d bytes\n", (int)do_list_queue_size); reset_do_list_queue(); } else { - do_list_queue = dlq; memset(do_list_queue + do_list_queue_size / 2, 0, do_list_queue_size / 2); } -- cgit