1998-11-05 19:51:34 +03:00
/*
2002-01-30 09:08:46 +03:00
* Unix SMB / CIFS implementation .
* SMB parameters and setup
1998-11-05 19:51:34 +03:00
* Copyright ( C ) Andrew Tridgell 1992 - 1998 Modified by Jeremy Allison 1995.
*
* This program is free software ; you can redistribute it and / or modify it under
* the terms of the GNU General Public License as published by the Free
2007-07-09 23:25:36 +04:00
* Software Foundation ; either version 3 of the License , or ( at your option )
1998-11-05 19:51:34 +03:00
* any later version .
*
* This program is distributed in the hope that it will be useful , but WITHOUT
* ANY WARRANTY ; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE . See the GNU General Public License for
* more details .
*
* You should have received a copy of the GNU General Public License along with
2007-07-10 09:23:25 +04:00
* this program ; if not , see < http : //www.gnu.org/licenses/>.
1998-11-05 19:51:34 +03:00
*/
# include "includes.h"
2015-10-12 16:57:34 +03:00
# include "lib/util/sys_rw.h"
1998-11-05 19:51:34 +03:00
2008-10-12 19:34:43 +04:00
/**
2004-09-01 05:33:55 +04:00
Load from a pipe into memory .
2008-10-12 19:34:43 +04:00
* */
2004-09-01 05:33:55 +04:00
2008-10-12 19:34:43 +04:00
static char * file_pload ( const char * syscmd , size_t * size )
2000-04-16 10:19:11 +04:00
{
2000-04-16 15:00:21 +04:00
int fd , n ;
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)
2006-03-07 09:31:04 +03:00
char * p ;
2007-11-16 01:19:52 +03:00
char buf [ 1024 ] ;
2000-04-16 15:00:21 +04:00
size_t total ;
2007-11-16 01:19:52 +03:00
2000-04-16 15:00:21 +04:00
fd = sys_popen ( syscmd ) ;
2004-09-01 05:33:55 +04:00
if ( fd = = - 1 ) {
return NULL ;
}
2000-04-16 15:00:21 +04:00
p = NULL ;
total = 0 ;
2013-01-24 02:39:09 +04:00
while ( ( n = sys_read ( fd , buf , sizeof ( buf ) ) ) > 0 ) {
2009-03-26 16:54:18 +03:00
p = talloc_realloc ( NULL , p , char , total + n + 1 ) ;
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)
2006-03-07 09:31:04 +03:00
if ( ! p ) {
2003-02-24 06:09:08 +03:00
DEBUG ( 0 , ( " file_pload: failed to expand buffer! \n " ) ) ;
2000-04-16 15:00:21 +04:00
close ( fd ) ;
return NULL ;
2004-09-01 05:33:55 +04:00
}
2000-04-16 15:00:21 +04:00
memcpy ( p + total , buf , n ) ;
total + = n ;
}
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)
2006-03-07 09:31:04 +03:00
if ( p ) {
p [ total ] = 0 ;
}
2000-04-16 15:00:21 +04:00
2003-02-24 06:09:08 +03:00
/* FIXME: Perhaps ought to check that the command completed
* successfully ( returned 0 ) ; if not the data may be
* truncated . */
2000-04-16 15:00:21 +04:00
sys_pclose ( fd ) ;
2004-09-01 05:33:55 +04:00
if ( size ) {
* size = total ;
}
2000-04-16 15:00:21 +04:00
return p ;
}
2004-09-01 05:33:55 +04:00
2001-04-13 04:37:00 +04:00
2008-10-12 19:34:43 +04:00
/**
2004-09-01 05:33:55 +04:00
Load a pipe into memory and return an array of pointers to lines in the data
2014-12-26 17:42:40 +03:00
must be freed with TALLOC_FREE .
2008-10-12 19:34:43 +04:00
* */
2004-09-01 05:33:55 +04:00
2008-10-12 19:34:43 +04:00
char * * file_lines_pload ( const char * syscmd , int * numlines )
2000-04-16 15:00:21 +04:00
{
char * p ;
size_t size ;
p = file_pload ( syscmd , & size ) ;
2004-09-01 05:33:55 +04:00
if ( ! p ) {
return NULL ;
}
2000-04-16 15:00:21 +04:00
2008-10-12 19:34:43 +04:00
return file_lines_parse ( p , size , numlines , NULL ) ;
2001-10-11 11:42:52 +04:00
}