1
0
mirror of https://github.com/samba-team/samba.git synced 2024-12-29 11:21:54 +03:00
samba-mirror/source3/lib/ldap_escape.c

92 lines
1.9 KiB
C
Raw Normal View History

/*
Unix SMB/CIFS implementation.
ldap filter argument escaping
Copyright (C) 1998, 1999, 2000 Luke Howard <lukeh@padl.com>,
Copyright (C) 2003 Andrew Bartlett <abartlet@samba.org>
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 Software Foundation; either version 2 of the License, or
(at your option) 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 this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "includes.h"
/**
* Escape a parameter to an LDAP filter string, so they cannot contain
* embeded ( ) * or \ chars which may cause it not to parse correctly.
*
* @param s The input string
*
* @return A string allocated with malloc(), containing the escaped string,
* and to be free()ed by the caller.
**/
char *escape_ldap_string_alloc(const char *s)
{
size_t len = strlen(s)+1;
char *output = (char *)SMB_MALLOC(len);
const char *sub;
int i = 0;
char *p = output;
if (output == NULL) {
return NULL;
}
while (*s)
{
switch (*s)
{
case '*':
sub = "\\2a";
break;
case '(':
sub = "\\28";
break;
case ')':
sub = "\\29";
break;
case '\\':
sub = "\\5c";
break;
default:
sub = NULL;
break;
}
if (sub) {
len = len + 3;
output = (char *)SMB_REALLOC(output, len);
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 (!output) {
return NULL;
}
p = &output[i];
strncpy (p, sub, 3);
p += 3;
i += 3;
} else {
*p = *s;
p++;
i++;
}
s++;
}
*p = '\0';
return output;
}