diff options
Diffstat (limited to 'source4/lib/util')
-rw-r--r-- | source4/lib/util/attr.h | 92 | ||||
-rw-r--r-- | source4/lib/util/config.mk | 3 | ||||
-rw-r--r-- | source4/lib/util/data_blob.c | 10 | ||||
-rw-r--r-- | source4/lib/util/data_blob.h | 1 | ||||
-rw-r--r-- | source4/lib/util/dprintf.c | 2 | ||||
-rw-r--r-- | source4/lib/util/fault.c | 4 | ||||
-rw-r--r-- | source4/lib/util/fault.m4 | 7 | ||||
-rw-r--r-- | source4/lib/util/ms_fnmatch.c | 5 | ||||
-rw-r--r-- | source4/lib/util/tests/file.c | 4 | ||||
-rw-r--r-- | source4/lib/util/tests/str.c | 121 | ||||
-rw-r--r-- | source4/lib/util/tests/strlist.c | 2 | ||||
-rw-r--r-- | source4/lib/util/time.c | 10 | ||||
-rw-r--r-- | source4/lib/util/time.h | 4 | ||||
-rw-r--r-- | source4/lib/util/util.c | 13 | ||||
-rw-r--r-- | source4/lib/util/util.h | 40 | ||||
-rw-r--r-- | source4/lib/util/util_pw.c | 45 | ||||
-rw-r--r-- | source4/lib/util/util_str.c | 82 | ||||
-rw-r--r-- | source4/lib/util/util_strlist.c | 28 | ||||
-rw-r--r-- | source4/lib/util/util_tdb.c | 5 | ||||
-rw-r--r-- | source4/lib/util/xfile.c | 2 |
20 files changed, 324 insertions, 156 deletions
diff --git a/source4/lib/util/attr.h b/source4/lib/util/attr.h new file mode 100644 index 0000000000..8f6c4f5d8a --- /dev/null +++ b/source4/lib/util/attr.h @@ -0,0 +1,92 @@ +/* + Unix SMB/CIFS implementation. + Samba utility functions + Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007 + + 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 3 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, see <http://www.gnu.org/licenses/>. +*/ + +#ifndef __UTIL_ATTR_H__ +#define __UTIL_ATTR_H__ + +#ifdef __GNUC__ +/** gcc attribute used on function parameters so that it does not emit + * warnings about them being unused. **/ +# define UNUSED(param) param __attribute__ ((unused)) +#else +# define UNUSED(param) param +/** Feel free to add definitions for other compilers here. */ +#endif + +#ifndef _PUBLIC_ +#ifdef HAVE_VISIBILITY_ATTR +# define _PUBLIC_ __attribute__((visibility("default"))) +#else +# define _PUBLIC_ +#endif +#endif + +#ifndef _DEPRECATED_ +#if (__GNUC__ >= 3) && (__GNUC_MINOR__ >= 1 ) +#define _DEPRECATED_ __attribute__ ((deprecated)) +#else +#define _DEPRECATED_ +#endif +#endif + +#ifndef _WARN_UNUSED_RESULT_ +#if (__GNUC__ >= 3) && (__GNUC_MINOR__ >= 1 ) +#define _WARN_UNUSED_RESULT_ __attribute__ ((warn_unused_result)) +#else +#define _WARN_UNUSED_RESULT_ +#endif +#endif + +#ifndef _NORETURN_ +#if (__GNUC__ >= 3) && (__GNUC_MINOR__ >= 1 ) +#define _NORETURN_ __attribute__ ((noreturn)) +#else +#define _NORETURN_ +#endif +#endif + +#ifndef _PURE_ +#if (__GNUC__ >= 3) && (__GNUC_MINOR__ >= 1) +#define _PURE_ __attribute__((pure)) +#else +#define _PURE_ +#endif +#endif + +#ifndef NONNULL +#if (__GNUC__ >= 3) && (__GNUC_MINOR__ >= 1) +#define NONNULL(param) param __attribute__((nonnull)) +#else +#define NONNULL(param) param +#endif +#endif + +#ifndef PRINTF_ATTRIBUTE +#if __GNUC__ >= 3 +/** Use gcc attribute to check printf fns. a1 is the 1-based index of + * the parameter containing the format, and a2 the index of the first + * argument. Note that some gcc 2.x versions don't handle this + * properly **/ +#define PRINTF_ATTRIBUTE(a1, a2) __attribute__ ((format (__printf__, a1, a2))) +#else +#define PRINTF_ATTRIBUTE(a1, a2) +#endif +#endif + +#endif /* __UTIL_ATTR_H__ */ diff --git a/source4/lib/util/config.mk b/source4/lib/util/config.mk index 702e3df5aa..0691bd7889 100644 --- a/source4/lib/util/config.mk +++ b/source4/lib/util/config.mk @@ -3,6 +3,7 @@ #SO_VERSION = 0 #DESCRIPTION = Generic utility functions PUBLIC_HEADERS = util.h \ + attr.h \ byteorder.h \ data_blob.h \ debug.h \ @@ -31,7 +32,7 @@ OBJ_FILES = xfile.o \ PUBLIC_DEPENDENCIES = \ LIBTALLOC LIBCRYPTO \ SOCKET_WRAPPER EXT_NSL \ - CHARSET + CHARSET EXECINFO [SUBSYSTEM::UNIX_PRIVS] PRIVATE_PROTO_HEADER = unix_privs.h diff --git a/source4/lib/util/data_blob.c b/source4/lib/util/data_blob.c index 117043f95c..b258e47bba 100644 --- a/source4/lib/util/data_blob.c +++ b/source4/lib/util/data_blob.c @@ -39,9 +39,9 @@ _PUBLIC_ DATA_BLOB data_blob_named(const void *p, size_t length, const char *nam } if (p) { - ret.data = talloc_memdup(NULL, p, length); + ret.data = (uint8_t *)talloc_memdup(NULL, p, length); } else { - ret.data = talloc_size(NULL, length); + ret.data = talloc_array(NULL, uint8_t, length); } if (ret.data == NULL) { ret.length = 0; @@ -175,7 +175,7 @@ _PUBLIC_ char *data_blob_hex_string(TALLOC_CTX *mem_ctx, const DATA_BLOB *blob) _PUBLIC_ DATA_BLOB data_blob_string_const(const char *str) { DATA_BLOB blob; - blob.data = discard_const(str); + blob.data = discard_const_p(uint8_t, str); blob.length = strlen(str); return blob; } @@ -187,7 +187,7 @@ _PUBLIC_ DATA_BLOB data_blob_string_const(const char *str) _PUBLIC_ DATA_BLOB data_blob_const(const void *p, size_t length) { DATA_BLOB blob; - blob.data = discard_const(p); + blob.data = discard_const_p(uint8_t, p); blob.length = length; return blob; } @@ -198,7 +198,7 @@ _PUBLIC_ DATA_BLOB data_blob_const(const void *p, size_t length) **/ _PUBLIC_ bool data_blob_realloc(TALLOC_CTX *mem_ctx, DATA_BLOB *blob, size_t length) { - blob->data = talloc_realloc_size(mem_ctx, blob->data, length); + blob->data = talloc_realloc(mem_ctx, blob->data, uint8_t, length); if (blob->data == NULL) return false; blob->length = length; diff --git a/source4/lib/util/data_blob.h b/source4/lib/util/data_blob.h index e39b2eaf45..1442438dd7 100644 --- a/source4/lib/util/data_blob.h +++ b/source4/lib/util/data_blob.h @@ -24,6 +24,7 @@ #endif #include <talloc.h> +#include <stdint.h> /* used to hold an arbitrary blob of data */ typedef struct datablob { diff --git a/source4/lib/util/dprintf.c b/source4/lib/util/dprintf.c index a7770f364d..91665e7bdd 100644 --- a/source4/lib/util/dprintf.c +++ b/source4/lib/util/dprintf.c @@ -48,7 +48,7 @@ _PUBLIC_ int d_vfprintf(FILE *f, const char *format, va_list ap) _PRINTF_ATTRIBU charset, but beware of it growing */ maxlen = ret*2; again: - p2 = malloc(maxlen); + p2 = (char *)malloc(maxlen); if (!p2) { SAFE_FREE(p); return -1; diff --git a/source4/lib/util/fault.c b/source4/lib/util/fault.c index 5cc9445407..e9cd040ee6 100644 --- a/source4/lib/util/fault.c +++ b/source4/lib/util/fault.c @@ -153,7 +153,7 @@ _PUBLIC_ _NORETURN_ void smb_panic(const char *why) /** report a fault **/ -static void fault_report(int sig) +_NORETURN_ static void fault_report(int sig) { static int counter; @@ -172,7 +172,7 @@ static void fault_report(int sig) /** catch serious errors **/ -static void sig_fault(int sig) +_NORETURN_ static void sig_fault(int sig) { if (fault_handlers.fault_handler) { /* we have a fault handler, call it. It may not return. */ diff --git a/source4/lib/util/fault.m4 b/source4/lib/util/fault.m4 index 6d2c4f2a6a..b24e63641c 100644 --- a/source4/lib/util/fault.m4 +++ b/source4/lib/util/fault.m4 @@ -1,2 +1,5 @@ -AC_CHECK_HEADER(execinfo.h) -AC_CHECK_FUNCS(backtrace) +AC_CHECK_HEADERS(execinfo.h) +AC_SEARCH_LIBS_EXT(backtrace, [execinfo], EXECINFO_LIBS) +AC_CHECK_FUNC_EXT(backtrace, $EXECINFO_LIBS) +SMB_EXT_LIB(EXECINFO,[${EXECINFO_LIBS}]) +SMB_ENABLE(EXECINFO) diff --git a/source4/lib/util/ms_fnmatch.c b/source4/lib/util/ms_fnmatch.c index 8e216b0226..73fb0e0966 100644 --- a/source4/lib/util/ms_fnmatch.c +++ b/source4/lib/util/ms_fnmatch.c @@ -201,11 +201,10 @@ int ms_fnmatch(const char *pattern, const char *string, enum protocol_types prot if (pattern[i] == '*' || pattern[i] == '<') count++; } - max_n = talloc_array(NULL, struct max_n, count); - if (!max_n) { + max_n = talloc_zero_array(NULL, struct max_n, count); + if (max_n == NULL) { return -1; } - memset(max_n, 0, sizeof(struct max_n) * count); ret = ms_fnmatch_core(pattern, string, max_n, strrchr(string, '.')); diff --git a/source4/lib/util/tests/file.c b/source4/lib/util/tests/file.c index 0fe117a300..fe87293671 100644 --- a/source4/lib/util/tests/file.c +++ b/source4/lib/util/tests/file.c @@ -88,10 +88,10 @@ struct torture_suite *torture_local_util_file(TALLOC_CTX *mem_ctx) struct torture_suite *suite = torture_suite_create(mem_ctx, "FILE"); torture_suite_add_simple_test(suite, "file_load_save", - test_file_load_save); + test_file_load_save); torture_suite_add_simple_test(suite, "afdgets", - test_afdgets); + test_afdgets); return suite; } diff --git a/source4/lib/util/tests/str.c b/source4/lib/util/tests/str.c new file mode 100644 index 0000000000..a219ef0891 --- /dev/null +++ b/source4/lib/util/tests/str.c @@ -0,0 +1,121 @@ +/* + Unix SMB/CIFS implementation. + + util_str testing + + Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007 + + 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 3 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, see <http://www.gnu.org/licenses/>. +*/ + +#include "includes.h" +#include "torture/ui.h" + +static bool test_string_sub_simple(struct torture_context *tctx) +{ + char tmp[100]; + safe_strcpy(tmp, "foobar", sizeof(tmp)); + string_sub(tmp, "foo", "bar", sizeof(tmp)); + torture_assert_str_equal(tctx, tmp, "barbar", "invalid sub"); + return true; +} + +static bool test_string_sub_multiple(struct torture_context *tctx) +{ + char tmp[100]; + safe_strcpy(tmp, "fooblafoo", sizeof(tmp)); + string_sub(tmp, "foo", "bar", sizeof(tmp)); + torture_assert_str_equal(tctx, tmp, "barblabar", "invalid sub"); + return true; +} + +static bool test_string_sub_longer(struct torture_context *tctx) +{ + char tmp[100]; + safe_strcpy(tmp, "foobla", sizeof(tmp)); + string_sub(tmp, "foo", "blie", sizeof(tmp)); + torture_assert_str_equal(tctx, tmp, "bliebla", "invalid sub"); + return true; +} + +static bool test_string_sub_shorter(struct torture_context *tctx) +{ + char tmp[100]; + safe_strcpy(tmp, "foobla", sizeof(tmp)); + string_sub(tmp, "foo", "bl", sizeof(tmp)); + torture_assert_str_equal(tctx, tmp, "blbla", "invalid sub"); + return true; +} + +static bool test_string_sub_special_char(struct torture_context *tctx) +{ + char tmp[100]; + safe_strcpy(tmp, "foobla", sizeof(tmp)); + string_sub(tmp, "foo", "%b;l", sizeof(tmp)); + torture_assert_str_equal(tctx, tmp, "_b_lbla", "invalid sub"); + return true; +} + +static bool test_string_sub_talloc_simple(struct torture_context *tctx) +{ + char *t; + + t = string_sub_talloc(tctx, "foobla", "foo", "bl"); + + torture_assert_str_equal(tctx, t, "blbla", "invalid sub"); + + return true; +} + +static bool test_string_sub_talloc_multiple(struct torture_context *tctx) +{ + char *t; + + t = string_sub_talloc(tctx, "fooblafoo", "foo", "aapnootmies"); + + torture_assert_str_equal(tctx, t, "aapnootmiesblaaapnootmies", + "invalid sub"); + + return true; +} + + + +struct torture_suite *torture_local_util_str(TALLOC_CTX *mem_ctx) +{ + struct torture_suite *suite = torture_suite_create(mem_ctx, "STR"); + + torture_suite_add_simple_test(suite, "string_sub_simple", + test_string_sub_simple); + + torture_suite_add_simple_test(suite, "string_sub_multiple", + test_string_sub_multiple); + + torture_suite_add_simple_test(suite, "string_sub_shorter", + test_string_sub_shorter); + + torture_suite_add_simple_test(suite, "string_sub_longer", + test_string_sub_longer); + + torture_suite_add_simple_test(suite, "string_sub_special_chars", + test_string_sub_special_char); + + torture_suite_add_simple_test(suite, "string_sub_talloc_simple", + test_string_sub_talloc_simple); + + torture_suite_add_simple_test(suite, "string_sub_talloc_multiple", + test_string_sub_talloc_multiple); + + return suite; +} diff --git a/source4/lib/util/tests/strlist.c b/source4/lib/util/tests/strlist.c index 41accd4bd2..437d9d741a 100644 --- a/source4/lib/util/tests/strlist.c +++ b/source4/lib/util/tests/strlist.c @@ -35,7 +35,7 @@ static const char *test_lists_shell_strings[] = { static bool test_lists_shell(struct torture_context *tctx, const void *test_data) { - const char *data = test_data; + const char *data = (const char *)test_data; const char **ret1, **ret2, *tmp; bool match = true; TALLOC_CTX *mem_ctx = tctx; diff --git a/source4/lib/util/time.c b/source4/lib/util/time.c index c800fffea0..fc51498009 100644 --- a/source4/lib/util/time.c +++ b/source4/lib/util/time.c @@ -269,7 +269,7 @@ _PUBLIC_ time_t pull_dos_date2(const uint8_t *date_ptr, int zone_offset) x2 = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16); SIVAL(&x,0,x2); - return pull_dos_date((void *)&x, zone_offset); + return pull_dos_date((const uint8_t *)&x, zone_offset); } /** @@ -374,14 +374,6 @@ _PUBLIC_ NTTIME pull_nttime(uint8_t *base, uint16_t offset) } /** - parse a nttime as a large integer in a string and return a NTTIME -*/ -_PUBLIC_ NTTIME nttime_from_string(const char *s) -{ - return strtoull(s, NULL, 0); -} - -/** return (tv1 - tv2) in microseconds */ _PUBLIC_ int64_t usec_time_diff(struct timeval *tv1, struct timeval *tv2) diff --git a/source4/lib/util/time.h b/source4/lib/util/time.h index a1b3facf24..557c5d4eab 100644 --- a/source4/lib/util/time.h +++ b/source4/lib/util/time.h @@ -19,6 +19,10 @@ #ifndef _SAMBA_TIME_H_ #define _SAMBA_TIME_H_ +#ifndef _PUBLIC_ +#define _PUBLIC_ +#endif + /* 64 bit time (100 nanosec) 1601 - cifs6.txt, section 3.5, page 30, 4 byte aligned */ typedef uint64_t NTTIME; diff --git a/source4/lib/util/util.c b/source4/lib/util/util.c index aa7a571585..624315f99e 100644 --- a/source4/lib/util/util.c +++ b/source4/lib/util/util.c @@ -190,28 +190,27 @@ _PUBLIC_ void msleep(unsigned int t) Get my own name, return in malloc'ed storage. **/ -_PUBLIC_ char* get_myname(void) +_PUBLIC_ char *get_myname(void) { char *hostname; - const int host_name_max = 255; char *p; - hostname = malloc(host_name_max+1); + hostname = (char *)malloc(MAXHOSTNAMELEN+1); *hostname = 0; /* get my host name */ - if (gethostname(hostname, host_name_max+1) == -1) { + if (gethostname(hostname, MAXHOSTNAMELEN+1) == -1) { DEBUG(0,("gethostname failed\n")); return NULL; } /* Ensure null termination. */ - hostname[host_name_max] = '\0'; + hostname[MAXHOSTNAMELEN] = '\0'; /* split off any parts after an initial . */ - p = strchr(hostname,'.'); + p = strchr(hostname, '.'); - if (p) + if (p != NULL) *p = 0; return hostname; diff --git a/source4/lib/util/util.h b/source4/lib/util/util.h index 8259e08512..1960aa6196 100644 --- a/source4/lib/util/util.h +++ b/source4/lib/util/util.h @@ -28,12 +28,12 @@ * @brief Helpful macros */ -struct substitute_context; struct smbsrv_tcon; extern const char *logfile; extern const char *panic_action; +#include "util/attr.h" #include "util/time.h" #include "util/data_blob.h" #include "util/xfile.h" @@ -73,7 +73,7 @@ extern const char *panic_action; */ #define SMB_ASSERT(b) do { if (!(b)) { \ DEBUG(0,("PANIC: assert failed at %s(%d)\n", __FILE__, __LINE__)); \ - smb_panic("assert failed"); abort(); }} while (0) + smb_panic("assert failed"); }} while (0) #ifndef SAFE_FREE /* Oh no this is also defined in tdb.h */ /** @@ -165,7 +165,7 @@ _PUBLIC_ void do_debug_header(int level, const char *location, const char *func) @note You should never have to call this function directly. Call the DEBUG() macro instead. */ -_PUBLIC_ void do_debug(const char *format, ...) _PRINTF_ATTRIBUTE(1,2); +_PUBLIC_ void do_debug(const char *format, ...) PRINTF_ATTRIBUTE(1,2); /** reopen the log file (usually called because the log file name might have changed) @@ -313,9 +313,9 @@ _PUBLIC_ char *generate_random_str(TALLOC_CTX *mem_ctx, size_t len); /* The following definitions come from lib/util/dprintf.c */ -_PUBLIC_ int d_vfprintf(FILE *f, const char *format, va_list ap) _PRINTF_ATTRIBUTE(2,0); -_PUBLIC_ int d_fprintf(FILE *f, const char *format, ...) _PRINTF_ATTRIBUTE(2,3); -_PUBLIC_ int d_printf(const char *format, ...) _PRINTF_ATTRIBUTE(1,2); +_PUBLIC_ int d_vfprintf(FILE *f, const char *format, va_list ap) PRINTF_ATTRIBUTE(2,0); +_PUBLIC_ int d_fprintf(FILE *f, const char *format, ...) PRINTF_ATTRIBUTE(2,3); +_PUBLIC_ int d_printf(const char *format, ...) PRINTF_ATTRIBUTE(1,2); _PUBLIC_ void display_set_stderr(void); /* The following definitions come from lib/util/util_str.c */ @@ -371,17 +371,6 @@ _PUBLIC_ void hex_encode(const unsigned char *buff_in, size_t len, char **out_he _PUBLIC_ char *hex_encode_talloc(TALLOC_CTX *mem_ctx, const unsigned char *buff_in, size_t len); /** - Free a string value. -**/ -_PUBLIC_ void string_free(char **s); - -/** - Set a string value, deallocating any existing space, and allocing the space - for the string -**/ -_PUBLIC_ bool string_set(char **dest, const char *src); - -/** Substitute a string for a pattern in another string. Make sure there is enough room! @@ -394,6 +383,10 @@ _PUBLIC_ bool string_set(char **dest, const char *src); **/ _PUBLIC_ void string_sub(char *s,const char *pattern, const char *insert, size_t len); + +_PUBLIC_ char *string_sub_talloc(TALLOC_CTX *mem_ctx, const char *s, + const char *pattern, const char *insert); + /** Similar to string_sub() but allows for any character to be substituted. Use with caution! @@ -562,11 +555,6 @@ _PUBLIC_ bool str_list_check(const char **list, const char *s); */ _PUBLIC_ bool str_list_check_ci(const char **list, const char *s); -/** - Check if a string is part of a list. -**/ -_PUBLIC_ bool in_list(const char *s, const char *list, bool casesensitive); - /* The following definitions come from lib/util/util_file.c */ @@ -620,8 +608,8 @@ _PUBLIC_ void file_lines_slashcont(char **lines); save a lump of data into a file. Mostly used for debugging */ _PUBLIC_ bool file_save(const char *fname, const void *packet, size_t length); -_PUBLIC_ int vfdprintf(int fd, const char *format, va_list ap) _PRINTF_ATTRIBUTE(2,0); -_PUBLIC_ int fdprintf(int fd, const char *format, ...) _PRINTF_ATTRIBUTE(2,3); +_PUBLIC_ int vfdprintf(int fd, const char *format, va_list ap) PRINTF_ATTRIBUTE(2,0); +_PUBLIC_ int fdprintf(int fd, const char *format, ...) PRINTF_ATTRIBUTE(2,3); _PUBLIC_ bool large_file_support(const char *path); /* The following definitions come from lib/util/util.c */ @@ -790,8 +778,6 @@ enum protocol_types { PROTOCOL_SMB2 }; - - int ms_fnmatch(const char *pattern, const char *string, enum protocol_types protocol); /** a generic fnmatch function - uses for non-CIFS pattern matching */ @@ -848,6 +834,6 @@ _PUBLIC_ int idr_remove(struct idr_context *idp, int id); /** Become a daemon, discarding the controlling terminal. **/ -_PUBLIC_ void become_daemon(bool Fork); +_PUBLIC_ void become_daemon(bool fork); #endif /* _SAMBA_UTIL_H_ */ diff --git a/source4/lib/util/util_pw.c b/source4/lib/util/util_pw.c index 0e0675571e..11e46ec4e3 100644 --- a/source4/lib/util/util_pw.c +++ b/source4/lib/util/util_pw.c @@ -21,37 +21,26 @@ #include "includes.h" -static struct passwd *alloc_copy_passwd(const struct passwd *from) +static struct passwd *alloc_copy_passwd(TALLOC_CTX *mem_ctx, + const struct passwd *from) { - struct passwd *ret = smb_xmalloc_p(struct passwd); - ZERO_STRUCTP(ret); - ret->pw_name = smb_xstrdup(from->pw_name); - ret->pw_passwd = smb_xstrdup(from->pw_passwd); - ret->pw_uid = from->pw_uid; - ret->pw_gid = from->pw_gid; - ret->pw_gecos = smb_xstrdup(from->pw_gecos); - ret->pw_dir = smb_xstrdup(from->pw_dir); - ret->pw_shell = smb_xstrdup(from->pw_shell); - return ret; -} + struct passwd *ret = talloc_zero(mem_ctx, struct passwd); -void passwd_free (struct passwd **buf) -{ - if (!*buf) { - DEBUG(0, ("attempted double-free of allocated passwd\n")); - return; - } + if (ret == NULL) + return NULL; - SAFE_FREE((*buf)->pw_name); - SAFE_FREE((*buf)->pw_passwd); - SAFE_FREE((*buf)->pw_gecos); - SAFE_FREE((*buf)->pw_dir); - SAFE_FREE((*buf)->pw_shell); + ret->pw_name = talloc_strdup(ret, from->pw_name); + ret->pw_passwd = talloc_strdup(ret, from->pw_passwd); + ret->pw_uid = from->pw_uid; + ret->pw_gid = from->pw_gid; + ret->pw_gecos = talloc_strdup(ret, from->pw_gecos); + ret->pw_dir = talloc_strdup(ret, from->pw_dir); + ret->pw_shell = talloc_strdup(ret, from->pw_shell); - SAFE_FREE(*buf); + return ret; } -struct passwd *getpwnam_alloc(const char *name) +struct passwd *getpwnam_alloc(TALLOC_CTX *mem_ctx, const char *name) { struct passwd *temp; @@ -66,10 +55,10 @@ struct passwd *getpwnam_alloc(const char *name) return NULL; } - return alloc_copy_passwd(temp); + return alloc_copy_passwd(mem_ctx, temp); } -struct passwd *getpwuid_alloc(uid_t uid) +struct passwd *getpwuid_alloc(TALLOC_CTX *mem_ctx, uid_t uid) { struct passwd *temp; @@ -84,5 +73,5 @@ struct passwd *getpwuid_alloc(uid_t uid) return NULL; } - return alloc_copy_passwd(temp); + return alloc_copy_passwd(mem_ctx, temp); } diff --git a/source4/lib/util/util_str.c b/source4/lib/util/util_str.c index 67e59474fd..9ea6403c52 100644 --- a/source4/lib/util/util_str.c +++ b/source4/lib/util/util_str.c @@ -23,7 +23,6 @@ #include "includes.h" #include "libcli/raw/smb.h" -#include "pstring.h" #include "system/locale.h" /** @@ -237,7 +236,7 @@ _PUBLIC_ void hex_encode(const unsigned char *buff_in, size_t len, char **out_he int i; char *hex_buffer; - *out_hex_buffer = smb_xmalloc((len*2)+1); + *out_hex_buffer = malloc_array_p(char, (len*2)+1); hex_buffer = *out_hex_buffer; for (i = 0; i < len; i++) @@ -261,39 +260,6 @@ _PUBLIC_ char *hex_encode_talloc(TALLOC_CTX *mem_ctx, const unsigned char *buff_ } /** - Set a string value, allocing the space for the string -**/ -static bool string_init(char **dest,const char *src) -{ - if (!src) src = ""; - - (*dest) = strdup(src); - if ((*dest) == NULL) { - DEBUG(0,("Out of memory in string_init\n")); - return false; - } - return true; -} - -/** - Free a string value. -**/ -_PUBLIC_ void string_free(char **s) -{ - if (s) SAFE_FREE(*s); -} - -/** - Set a string value, deallocating any existing space, and allocing the space - for the string -**/ -_PUBLIC_ bool string_set(char **dest, const char *src) -{ - string_free(dest); - return string_init(dest,src); -} - -/** Substitute a string for a pattern in another string. Make sure there is enough room! @@ -305,10 +271,10 @@ _PUBLIC_ bool string_set(char **dest, const char *src) use of len==0 which was for no length checks to be done. **/ -_PUBLIC_ void string_sub(char *s,const char *pattern, const char *insert, size_t len) +_PUBLIC_ void string_sub(char *s, const char *pattern, const char *insert, size_t len) { char *p; - ssize_t ls,lp,li, i; + ssize_t ls, lp, li, i; if (!insert || !pattern || !*pattern || !s) return; @@ -320,7 +286,7 @@ _PUBLIC_ void string_sub(char *s,const char *pattern, const char *insert, size_t if (len == 0) len = ls + 1; /* len is number of *bytes* */ - while (lp <= ls && (p = strstr(s,pattern))) { + while (lp <= ls && (p = strstr(s, pattern))) { if (ls + (li-lp) >= len) { DEBUG(0,("ERROR: string overflow by %d in string_sub(%.50s, %d)\n", (int)(ls + (li-lp) - len), @@ -351,6 +317,42 @@ _PUBLIC_ void string_sub(char *s,const char *pattern, const char *insert, size_t } } +/** + * Talloc'ed version of string_sub + */ +_PUBLIC_ char *string_sub_talloc(TALLOC_CTX *mem_ctx, const char *s, + const char *pattern, const char *insert) +{ + const char *p; + char *ret; + size_t len, alloc_len; + + if (insert == NULL || pattern == NULL || !*pattern || s == NULL) + return NULL; + + /* determine length needed */ + len = strlen(s); + + for (p = strstr(s, pattern); p != NULL; + p = strstr(p+strlen(pattern), pattern)) { + len += strlen(insert) - strlen(pattern); + } + + alloc_len = MAX(len, strlen(s))+1; + ret = talloc_array(mem_ctx, char, alloc_len); + if (ret == NULL) + return NULL; + strncpy(ret, s, alloc_len); + string_sub(ret, pattern, insert, alloc_len); + + ret = talloc_realloc(mem_ctx, ret, char, len+1); + if (ret == NULL) + return NULL; + + SMB_ASSERT(ret[len] == '\0'); + + return ret; +} /** Similar to string_sub() but allows for any character to be substituted. @@ -457,7 +459,7 @@ _PUBLIC_ const char *str_format_nbt_domain(TALLOC_CTX *mem_ctx, const char *s) if (!s || !*s) { return talloc_strdup(mem_ctx, ""); } - ret = talloc_size(mem_ctx, strlen(s)+2); + ret = talloc_array(mem_ctx, char, strlen(s)+2); if (!ret) { return ret; } @@ -566,7 +568,7 @@ _PUBLIC_ char *attrib_string(TALLOC_CTX *mem_ctx, uint32_t attrib) }; char *ret; - ret = talloc_size(mem_ctx, ARRAY_SIZE(attr_strs)+1); + ret = talloc_array(mem_ctx, char, ARRAY_SIZE(attr_strs)+1); if (!ret) { return NULL; } diff --git a/source4/lib/util/util_strlist.c b/source4/lib/util/util_strlist.c index ab73fb2d15..1f1cc17d00 100644 --- a/source4/lib/util/util_strlist.c +++ b/source4/lib/util/util_strlist.c @@ -19,7 +19,6 @@ */ #include "includes.h" -#include "pstring.h" #include "system/locale.h" /** @@ -152,7 +151,7 @@ _PUBLIC_ char *str_list_join(TALLOC_CTX *mem_ctx, const char **list, char sepera ret = talloc_strdup(mem_ctx, list[0]); for (i = 1; list[i]; i++) { - ret = talloc_asprintf_append(ret, "%c%s", seperator, list[i]); + ret = talloc_asprintf_append_buffer(ret, "%c%s", seperator, list[i]); } return ret; @@ -175,9 +174,9 @@ _PUBLIC_ char *str_list_join_shell(TALLOC_CTX *mem_ctx, const char **list, char for (i = 1; list[i]; i++) { if (strchr(list[i], ' ') || strlen(list[i]) == 0) - ret = talloc_asprintf_append(ret, "%c\"%s\"", sep, list[i]); + ret = talloc_asprintf_append_buffer(ret, "%c\"%s\"", sep, list[i]); else - ret = talloc_asprintf_append(ret, "%c%s", sep, list[i]); + ret = talloc_asprintf_append_buffer(ret, "%c%s", sep, list[i]); } return ret; @@ -300,25 +299,4 @@ _PUBLIC_ bool str_list_check_ci(const char **list, const char *s) return false; } -/** - Check if a string is part of a list. -**/ -_PUBLIC_ bool in_list(const char *s, const char *list, bool casesensitive) -{ - pstring tok; - const char *p=list; - if (!list) - return false; - - while (next_token(&p,tok,LIST_SEP,sizeof(tok))) { - if (casesensitive) { - if (strcmp(tok,s) == 0) - return true; - } else { - if (strcasecmp_m(tok,s) == 0) - return true; - } - } - return false; -} diff --git a/source4/lib/util/util_tdb.c b/source4/lib/util/util_tdb.c index 1112f1587c..d7bddbde01 100644 --- a/source4/lib/util/util_tdb.c +++ b/source4/lib/util/util_tdb.c @@ -22,6 +22,7 @@ #include "includes.h" #include "lib/tdb/include/tdb.h" #include "pstring.h" +#include "lib/util/util_tdb.h" /* these are little tdb utility functions that are meant to make dealing with a tdb database a little less cumbersome in Samba */ @@ -127,7 +128,7 @@ int tdb_store_int32_byblob(struct tdb_context *tdb, const char *keystr, size_t l int32_t v_store; SIVAL(&v_store,0,v); - data.dptr = (void *)&v_store; + data.dptr = (unsigned char *)&v_store; data.dsize = sizeof(int32_t); return tdb_store(tdb, key, data, TDB_REPLACE); @@ -187,7 +188,7 @@ bool tdb_store_uint32_byblob(struct tdb_context *tdb, const char *keystr, size_t bool ret = true; SIVAL(&v_store, 0, value); - data.dptr = (void *)&v_store; + data.dptr = (unsigned char *)&v_store; data.dsize = sizeof(uint32_t); if (tdb_store(tdb, key, data, TDB_REPLACE) == -1) diff --git a/source4/lib/util/xfile.c b/source4/lib/util/xfile.c index 8b90e30868..a016031a77 100644 --- a/source4/lib/util/xfile.c +++ b/source4/lib/util/xfile.c @@ -85,7 +85,7 @@ static int x_allocate_buffer(XFILE *f) { if (f->buf) return 1; if (f->bufsize == 0) return 0; - f->buf = malloc(f->bufsize); + f->buf = (char *)malloc(f->bufsize); if (!f->buf) return 0; f->next = f->buf; return 1; |