1
0
mirror of https://github.com/samba-team/samba.git synced 2024-12-29 11:21:54 +03:00
samba-mirror/source3/modules
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
..
charset_macosxfs.c Patch from Benjamin Riefenstahl <Benjamin.Riefenstahl@epost.de> to add 2003-11-26 20:58:51 +00:00
CP437.c Merge from 3.0: 2003-09-19 06:23:51 +00:00
CP850.c Merge from 3.0: 2003-09-19 06:23:51 +00:00
developer.c r10819: merging a couple of fixes from trunk 2007-10-10 11:04:54 -05:00
getdate.c r13384: Adding in some more SuSE patches 2007-10-10 11:09:57 -05:00
getdate.h sync 3.0 into HEAD for the last time 2003-09-09 04:07:32 +00:00
getdate.y r13384: Adding in some more SuSE patches 2007-10-10 11:09:57 -05:00
vfs_afsacl.c r12051: Merge across the lookup_name and lookup_sid work. Lets see how the build farm 2007-10-10 11:05:43 -05:00
vfs_audit.c r10619: Allow syslog facility and priority to be set via 2007-10-10 11:04:48 -05:00
vfs_cap.c r9483: Changed DIR to SMB_STRUCT_DIR because of the amazing stupidity of a UNIX vendor 2007-10-10 11:01:11 -05:00
vfs_catia.c r9483: Changed DIR to SMB_STRUCT_DIR because of the amazing stupidity of a UNIX vendor 2007-10-10 11:01:11 -05:00
vfs_default_quota.c r10106: Fix typos. Oops, more fixes. 2007-10-10 11:03:33 -05:00
vfs_expand_msdfs.c r4864: Remove unused var. 2007-10-10 10:54:00 -05:00
vfs_extd_audit.c r10619: Allow syslog facility and priority to be set via 2007-10-10 11:04:48 -05:00
vfs_fake_perms.c r13293: Rather a big patch I'm afraid, but this should fix bug #3347 2007-10-10 11:06:21 -05:00
vfs_full_audit.c r13028: Fix for #3419 - vfs_full_audit *never* worked 2007-10-10 11:06:11 -05:00
vfs_netatalk.c r9483: Changed DIR to SMB_STRUCT_DIR because of the amazing stupidity of a UNIX vendor 2007-10-10 11:01:11 -05:00
vfs_readonly.c sync 3.0 into HEAD for the last time 2003-09-09 04:07:32 +00:00
vfs_recycle.c r13224: better to cast the return too 2007-10-10 11:06:19 -05:00
vfs_shadow_copy.c r13915: Fixed a very interesting class of realloc() bugs found by Coverity. 2007-10-10 11:10:59 -05:00
weird.c r10656: BIG merge from trunk. Features not copied over 2007-10-10 11:04:48 -05:00