svn commit: samba r13915 - in branches/SAMBA_3_0/source: client groupdb include lib libsmb locking modules nsswitch param passdb printing registry rpc_parse rpc_server sam smbd tdb torture utils web

jra at samba.org jra at samba.org
Tue Mar 7 06:31:19 GMT 2006


Author: jra
Date: 2006-03-07 06:31:04 +0000 (Tue, 07 Mar 2006)
New Revision: 13915

WebSVN: http://websvn.samba.org/cgi-bin/viewcvs.cgi?view=rev&root=samba&rev=13915

Log:
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.

Modified:
   branches/SAMBA_3_0/source/client/client.c
   branches/SAMBA_3_0/source/client/clitar.c
   branches/SAMBA_3_0/source/client/smbctool.c
   branches/SAMBA_3_0/source/groupdb/mapping.c
   branches/SAMBA_3_0/source/include/smb_macros.h
   branches/SAMBA_3_0/source/lib/charcnv.c
   branches/SAMBA_3_0/source/lib/ldap_escape.c
   branches/SAMBA_3_0/source/lib/sysacls.c
   branches/SAMBA_3_0/source/lib/system_smbd.c
   branches/SAMBA_3_0/source/lib/util.c
   branches/SAMBA_3_0/source/lib/util_file.c
   branches/SAMBA_3_0/source/lib/util_sid.c
   branches/SAMBA_3_0/source/lib/util_str.c
   branches/SAMBA_3_0/source/lib/wins_srv.c
   branches/SAMBA_3_0/source/libsmb/asn1.c
   branches/SAMBA_3_0/source/libsmb/clilist.c
   branches/SAMBA_3_0/source/libsmb/clireadwrite.c
   branches/SAMBA_3_0/source/libsmb/clitrans.c
   branches/SAMBA_3_0/source/libsmb/namequery.c
   branches/SAMBA_3_0/source/libsmb/spnego.c
   branches/SAMBA_3_0/source/locking/brlock.c
   branches/SAMBA_3_0/source/locking/posix.c
   branches/SAMBA_3_0/source/modules/vfs_shadow_copy.c
   branches/SAMBA_3_0/source/nsswitch/wb_client.c
   branches/SAMBA_3_0/source/nsswitch/winbindd_cache.c
   branches/SAMBA_3_0/source/nsswitch/winbindd_group.c
   branches/SAMBA_3_0/source/nsswitch/winbindd_user.c
   branches/SAMBA_3_0/source/param/loadparm.c
   branches/SAMBA_3_0/source/param/params.c
   branches/SAMBA_3_0/source/passdb/pdb_ldap.c
   branches/SAMBA_3_0/source/printing/nt_printing.c
   branches/SAMBA_3_0/source/printing/print_cups.c
   branches/SAMBA_3_0/source/printing/print_iprint.c
   branches/SAMBA_3_0/source/printing/printing.c
   branches/SAMBA_3_0/source/registry/reg_db.c
   branches/SAMBA_3_0/source/registry/reg_perfcount.c
   branches/SAMBA_3_0/source/registry/reg_printing.c
   branches/SAMBA_3_0/source/rpc_parse/parse_buffer.c
   branches/SAMBA_3_0/source/rpc_parse/parse_prs.c
   branches/SAMBA_3_0/source/rpc_parse/parse_spoolss.c
   branches/SAMBA_3_0/source/rpc_server/srv_pipe.c
   branches/SAMBA_3_0/source/rpc_server/srv_spoolss_nt.c
   branches/SAMBA_3_0/source/sam/idmap_rid.c
   branches/SAMBA_3_0/source/smbd/lanman.c
   branches/SAMBA_3_0/source/smbd/msdfs.c
   branches/SAMBA_3_0/source/smbd/nttrans.c
   branches/SAMBA_3_0/source/smbd/password.c
   branches/SAMBA_3_0/source/smbd/session.c
   branches/SAMBA_3_0/source/smbd/trans2.c
   branches/SAMBA_3_0/source/tdb/tdbutil.c
   branches/SAMBA_3_0/source/torture/nsstest.c
   branches/SAMBA_3_0/source/utils/net_rpc.c
   branches/SAMBA_3_0/source/utils/net_rpc_samsync.c
   branches/SAMBA_3_0/source/utils/net_status.c
   branches/SAMBA_3_0/source/web/cgi.c


Changeset:
Sorry, the patch is too large (3172 lines) to include; please use WebSVN to see it!
WebSVN: http://websvn.samba.org/cgi-bin/viewcvs.cgi?view=rev&root=samba&rev=13915


More information about the samba-cvs mailing list