summaryrefslogtreecommitdiffstats
path: root/source/libsmb/clilist.c
diff options
context:
space:
mode:
authorJeremy Allison <jra@samba.org>2006-03-07 06:31:04 +0000
committerJeremy Allison <jra@samba.org>2006-03-07 06:31:04 +0000
commitca41d9224a8ec8a0fa65101dcd2034de30c43adc (patch)
tree9784cdfe1bb543a79acd7394f1c1eac6e33f9509 /source/libsmb/clilist.c
parentd3cdea0347ce7df85cc273f687617be3aa960e8c (diff)
downloadsamba-ca41d9224a8ec8a0fa65101dcd2034de30c43adc.tar.gz
samba-ca41d9224a8ec8a0fa65101dcd2034de30c43adc.tar.xz
samba-ca41d9224a8ec8a0fa65101dcd2034de30c43adc.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/libsmb/clilist.c')
-rw-r--r--source/libsmb/clilist.c17
1 files changed, 6 insertions, 11 deletions
diff --git a/source/libsmb/clilist.c b/source/libsmb/clilist.c
index 252dafcfa8b..1bd30c36e3c 100644
--- a/source/libsmb/clilist.c
+++ b/source/libsmb/clilist.c
@@ -179,7 +179,7 @@ int cli_list_new(struct cli_state *cli,const char *Mask,uint16 attribute,
pstring mask;
file_info finfo;
int i;
- char *tdl, *dirlist = NULL;
+ char *dirlist = NULL;
int dirlist_len = 0;
int total_received = -1;
BOOL First = True;
@@ -338,15 +338,13 @@ int cli_list_new(struct cli_state *cli,const char *Mask,uint16 attribute,
/* grab the data for later use */
/* and add them to the dirlist pool */
- tdl = SMB_REALLOC(dirlist,dirlist_len + data_len);
+ dirlist = SMB_REALLOC(dirlist,dirlist_len + data_len);
- if (!tdl) {
+ if (!dirlist) {
DEBUG(0,("cli_list_new: Failed to expand dirlist\n"));
SAFE_FREE(rdata);
SAFE_FREE(rparam);
break;
- } else {
- dirlist = tdl;
}
memcpy(dirlist+dirlist_len,p,data_len);
@@ -421,7 +419,7 @@ int cli_list_old(struct cli_state *cli,const char *Mask,uint16 attribute,
int num_asked = (cli->max_xmit - 100)/DIR_STRUCT_SIZE;
int num_received = 0;
int i;
- char *tdl, *dirlist = NULL;
+ char *dirlist = NULL;
pstring mask;
ZERO_ARRAY(status);
@@ -466,14 +464,11 @@ int cli_list_old(struct cli_state *cli,const char *Mask,uint16 attribute,
first = False;
- tdl = SMB_REALLOC(dirlist,(num_received + received)*DIR_STRUCT_SIZE);
-
- if (!tdl) {
+ dirlist = SMB_REALLOC(dirlist,(num_received + received)*DIR_STRUCT_SIZE);
+ if (!dirlist) {
DEBUG(0,("cli_list_old: failed to expand dirlist"));
- SAFE_FREE(dirlist);
return 0;
}
- else dirlist = tdl;
p = smb_buf(cli->inbuf) + 3;