1
0
mirror of https://github.com/samba-team/samba.git synced 2025-01-12 09:18:10 +03:00
samba-mirror/source3/utils
Jeremy Allison 894358a8f3 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 1d710d06a2)
2007-10-10 11:10:59 -05:00
..
debug2html.c r7902: Fix the build 2007-10-10 10:58:02 -05:00
debugparse.c r12111: Fix the "everything" build by re-adding debug2html. Oops. 2007-10-10 11:05:45 -05:00
eventlogadm.c r13212: r12414@cabra: derrell | 2006-01-28 17:52:17 -0500 2007-10-10 11:06:18 -05:00
log2pcaphex.c r10656: BIG merge from trunk. Features not copied over 2007-10-10 11:04:48 -05:00
net_ads_cldap.c r12986: Use d_fprintf(stderr, ...) for any error message in net. 2007-10-10 11:06:09 -05:00
net_ads_gpo.c r13316: Let the carnage begin.... 2007-10-10 11:06:23 -05:00
net_ads.c r12986: Use d_fprintf(stderr, ...) for any error message in net. 2007-10-10 11:06:09 -05:00
net_cache.c r12986: Use d_fprintf(stderr, ...) for any error message in net. 2007-10-10 11:06:09 -05:00
net_groupmap.c r13648: Duh. 2007-10-10 11:10:21 -05:00
net_help.c r13316: Let the carnage begin.... 2007-10-10 11:06:23 -05:00
net_idmap.c r12986: Use d_fprintf(stderr, ...) for any error message in net. 2007-10-10 11:06:09 -05:00
net_lookup.c r13316: Let the carnage begin.... 2007-10-10 11:06:23 -05:00
net_rap.c r12986: Use d_fprintf(stderr, ...) for any error message in net. 2007-10-10 11:06:09 -05:00
net_rpc_join.c r13641: Finish fix for #3510. Don't use client schannel when told 2007-10-10 11:10:20 -05:00
net_rpc_printer.c r13864: Some cleanup and the samr set security object function client-side. 2007-10-10 11:10:57 -05:00
net_rpc_registry.c r12986: Use d_fprintf(stderr, ...) for any error message in net. 2007-10-10 11:06:09 -05:00
net_rpc_rights.c r13316: Let the carnage begin.... 2007-10-10 11:06:23 -05:00
net_rpc_samsync.c r13915: Fixed a very interesting class of realloc() bugs found by Coverity. 2007-10-10 11:10:59 -05:00
net_rpc_service.c r12986: Use d_fprintf(stderr, ...) for any error message in net. 2007-10-10 11:06:09 -05:00
net_rpc_sh_acct.c r13351: Fix copyright 2007-10-10 11:06:26 -05:00
net_rpc_shell.c r13579: Next try to fix the AIX build. Thanks to Björn for nagging... 2007-10-10 11:10:15 -05:00
net_rpc.c r13915: Fixed a very interesting class of realloc() bugs found by Coverity. 2007-10-10 11:10:59 -05:00
net_sam.c r13846: Take care of system that do not have LDAP libraries 2007-10-10 11:10:57 -05:00
net_status.c r13915: Fixed a very interesting class of realloc() bugs found by Coverity. 2007-10-10 11:10:59 -05:00
net_time.c r12986: Use d_fprintf(stderr, ...) for any error message in net. 2007-10-10 11:06:09 -05:00
net_usershare.c r13594: Got sense of NTSTATUS check reversed. 2007-10-10 11:10:18 -05:00
net_util.c r13316: Let the carnage begin.... 2007-10-10 11:06:23 -05:00
net.c r13861: Avoid "net rpc join" segfaulting when storing the servername in the 2007-10-10 11:10:57 -05:00
net.h r13316: Let the carnage begin.... 2007-10-10 11:06:23 -05:00
netlookup.c r13502: Fix error messages for usershares when smbd is not 2007-10-10 11:10:07 -05:00
nmblookup.c r13530: Fix from William Jojo for #1970. Make nmblookup do 2007-10-10 11:10:10 -05:00
ntlm_auth_diagnostics.c r11137: Compile with only 2 warnings (I'm still working on that code) on a gcc4 2007-10-10 11:05:02 -05:00
ntlm_auth.c r13571: Replace all calls to talloc_free() with thye TALLOC_FREE() 2007-10-10 11:10:14 -05:00
ntlm_auth.h r2835: Since we always have -I. and -I$(srcdir) in CFLAGS, we can get rid of 2007-10-10 10:52:55 -05:00
passwd_util.c r13553: Fix all our warnings at -O6 on an x86_64 box. 2007-10-10 11:10:13 -05:00
pdbedit.c r13679: Commiting the rm_primary_group.patch posted on samba-technical 2007-10-10 11:10:23 -05:00
profiles.c r13486: Two more -- fix bug 3503 2007-10-10 11:10:05 -05:00
rpccheck.c Reverse previous patch from Stefan and me after comments by Andrew Bartlett 2003-05-10 11:49:51 +00:00
smbcacls.c r13316: Let the carnage begin.... 2007-10-10 11:06:23 -05:00
smbcontrol.c r13695: Make code consistent with documentation. :-) 2007-10-10 11:10:24 -05:00
smbcquotas.c r13316: Let the carnage begin.... 2007-10-10 11:06:23 -05:00
smbfilter.c r13212: r12414@cabra: derrell | 2006-01-28 17:52:17 -0500 2007-10-10 11:06:18 -05:00
smbget.c r12555: Fix more load_case_table swegfaults. Arggg. 2007-10-10 11:05:59 -05:00
smbpasswd.c r13590: * replace all pdb_init_sam[_talloc]() calls with samu_new() 2007-10-10 11:10:16 -05:00
smbtree.c r13212: r12414@cabra: derrell | 2006-01-28 17:52:17 -0500 2007-10-10 11:06:18 -05:00
smbw_sample.c r13212: r12414@cabra: derrell | 2006-01-28 17:52:17 -0500 2007-10-10 11:06:18 -05:00
status.c r13262: Arrgggg. Fix smbstatus and swat status to ignore 2007-10-10 11:06:20 -05:00
testparm.c r13212: r12414@cabra: derrell | 2006-01-28 17:52:17 -0500 2007-10-10 11:06:18 -05:00