summaryrefslogtreecommitdiffstats
path: root/source/web
diff options
context:
space:
mode:
authorJeremy Allison <jra@samba.org>2006-03-07 06:31:04 +0000
committerGerald (Jerry) Carter <jerry@samba.org>2007-10-10 11:10:59 -0500
commit1d710d06a214f3f1740e80e0bffd6aab44aac2b0 (patch)
tree9784cdfe1bb543a79acd7394f1c1eac6e33f9509 /source/web
parent3f671033bca7a025f9639728a0a0a0adede6ed35 (diff)
downloadsamba-1d710d06a214f3f1740e80e0bffd6aab44aac2b0.tar.gz
samba-1d710d06a214f3f1740e80e0bffd6aab44aac2b0.tar.xz
samba-1d710d06a214f3f1740e80e0bffd6aab44aac2b0.zip
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.
Diffstat (limited to 'source/web')
-rw-r--r--source/web/cgi.c2
1 files changed, 1 insertions, 1 deletions
diff --git a/source/web/cgi.c b/source/web/cgi.c
index d1cd38eb512..b764b6d6283 100644
--- a/source/web/cgi.c
+++ b/source/web/cgi.c
@@ -59,7 +59,7 @@ static char *grab_line(FILE *f, int *cl)
char *ret2;
if (len == 0) len = 1024;
else len *= 2;
- ret2 = (char *)SMB_REALLOC(ret, len);
+ ret2 = (char *)SMB_REALLOC_KEEP_OLD_ON_ERROR(ret, len);
if (!ret2) return ret;
ret = ret2;
}