1
0
mirror of https://github.com/samba-team/samba.git synced 2024-12-27 03:21:53 +03:00
samba-mirror/source3/torture
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
..
cmd_vfs.c r8682: fix vfstest, thanks to Rainer Link for spotting this one 2007-10-10 11:00:14 -05:00
denytest.c r8572: Remove crufty #define NO_SYSLOG as it's not used at all anymore. 2007-10-10 11:00:11 -05:00
locktest2.c r13212: r12414@cabra: derrell | 2006-01-28 17:52:17 -0500 2007-10-10 11:06:18 -05:00
locktest.c r13799: Make locktest debug a little easier to read. 2007-10-10 11:10:54 -05:00
mangle_test.c r10656: BIG merge from trunk. Features not copied over 2007-10-10 11:04:48 -05:00
masktest.c r13212: r12414@cabra: derrell | 2006-01-28 17:52:17 -0500 2007-10-10 11:06:18 -05:00
msgtest.c r13212: r12414@cabra: derrell | 2006-01-28 17:52:17 -0500 2007-10-10 11:06:18 -05:00
nbio.c r8572: Remove crufty #define NO_SYSLOG as it's not used at all anymore. 2007-10-10 11:00:11 -05:00
nsstest.c r13915: Fixed a very interesting class of realloc() bugs found by Coverity. 2007-10-10 11:10:59 -05:00
rpctorture.c r13212: r12414@cabra: derrell | 2006-01-28 17:52:17 -0500 2007-10-10 11:06:18 -05:00
samtest.h - Don't put pointer to sam_domain_handle in sam_methods but single domainsid and domainname 2002-09-24 20:18:39 +00:00
scanner.c r8572: Remove crufty #define NO_SYSLOG as it's not used at all anymore. 2007-10-10 11:00:11 -05:00
smbiconv.c Fix for #150. 2003-08-26 19:48:16 +00:00
t_asn1.c r10656: BIG merge from trunk. Features not copied over 2007-10-10 11:04:48 -05:00
t_doschar.c Test harness that exercises check_dos_char() 2003-04-04 03:07:07 +00:00
t_push_ucs2.c r13212: r12414@cabra: derrell | 2006-01-28 17:52:17 -0500 2007-10-10 11:06:18 -05:00
t_strappend.c r10656: BIG merge from trunk. Features not copied over 2007-10-10 11:04:48 -05:00
t_strcmp.c r13212: r12414@cabra: derrell | 2006-01-28 17:52:17 -0500 2007-10-10 11:06:18 -05:00
t_stringoverflow.c The new string macros catch a bug at compile that previously only 2003-03-18 05:31:52 +00:00
t_strstr.c r13212: r12414@cabra: derrell | 2006-01-28 17:52:17 -0500 2007-10-10 11:06:18 -05:00
torture.c r13212: r12414@cabra: derrell | 2006-01-28 17:52:17 -0500 2007-10-10 11:06:18 -05:00
utable.c r8572: Remove crufty #define NO_SYSLOG as it's not used at all anymore. 2007-10-10 11:00:11 -05:00
vfstest.c r13212: r12414@cabra: derrell | 2006-01-28 17:52:17 -0500 2007-10-10 11:06:18 -05:00
vfstest.h Fix VFS layer: 2003-05-11 23:34:18 +00:00