r13915: Fixed a very interesting class of realloc() bugs found by Coverity.
authorJeremy Allison <jra@samba.org>
Tue, 7 Mar 2006 06:31:04 +0000 (06:31 +0000)
committerGerald (Jerry) Carter <jerry@samba.org>
Wed, 10 Oct 2007 16:10:59 +0000 (11:10 -0500)
commit894358a8f3e338b339b6c37233edef794b312087
tree2dade0771837ba267d365af24d551c3084f0f948
parent1d5ed2bde9a67083c9b73c7eb6fb966c0e824ab2
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)
55 files changed:
source3/client/client.c
source3/client/clitar.c
source3/client/smbctool.c
source3/groupdb/mapping.c
source3/include/smb_macros.h
source3/lib/charcnv.c
source3/lib/ldap_escape.c
source3/lib/sysacls.c
source3/lib/system_smbd.c
source3/lib/util.c
source3/lib/util_file.c
source3/lib/util_sid.c
source3/lib/util_str.c
source3/lib/wins_srv.c
source3/libsmb/asn1.c
source3/libsmb/clilist.c
source3/libsmb/clireadwrite.c
source3/libsmb/clitrans.c
source3/libsmb/namequery.c
source3/libsmb/spnego.c
source3/locking/brlock.c
source3/locking/posix.c
source3/modules/vfs_shadow_copy.c
source3/nsswitch/wb_client.c
source3/nsswitch/winbindd_cache.c
source3/nsswitch/winbindd_group.c
source3/nsswitch/winbindd_user.c
source3/param/loadparm.c
source3/param/params.c
source3/passdb/pdb_ldap.c
source3/printing/nt_printing.c
source3/printing/print_cups.c
source3/printing/print_iprint.c
source3/printing/printing.c
source3/registry/reg_db.c
source3/registry/reg_perfcount.c
source3/registry/reg_printing.c
source3/rpc_parse/parse_buffer.c
source3/rpc_parse/parse_prs.c
source3/rpc_parse/parse_spoolss.c
source3/rpc_server/srv_pipe.c
source3/rpc_server/srv_spoolss_nt.c
source3/sam/idmap_rid.c
source3/smbd/lanman.c
source3/smbd/msdfs.c
source3/smbd/nttrans.c
source3/smbd/password.c
source3/smbd/session.c
source3/smbd/trans2.c
source3/tdb/tdbutil.c
source3/torture/nsstest.c
source3/utils/net_rpc.c
source3/utils/net_rpc_samsync.c
source3/utils/net_status.c
source3/web/cgi.c