#include "includes.h"
+/* Max allowable allococation - 256mb - 0x10000000 */
+#define MAX_ALLOC_SIZE (1024*1024*256)
+
#if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
#ifdef WITH_NISPLUS_HOME
#ifdef BROKEN_NISPLUS_INCLUDE_FILES
#endif /* WITH_NISPLUS_HOME */
#endif /* HAVE_NETGROUP && WITH_AUTOMOUNT */
-int Protocol = PROTOCOL_COREPLUS;
+enum protocol_types Protocol = PROTOCOL_COREPLUS;
/* a default finfo structure to ensure all fields are sensible */
file_info def_finfo = {-1,0,0,0,0,0,0,"",""};
BOOL set_global_myname(const char *myname)
{
SAFE_FREE(smb_myname);
- smb_myname = strdup(myname);
+ smb_myname = SMB_STRDUP(myname);
if (!smb_myname)
return False;
strupper_m(smb_myname);
BOOL set_global_myworkgroup(const char *myworkgroup)
{
SAFE_FREE(smb_myworkgroup);
- smb_myworkgroup = strdup(myworkgroup);
+ smb_myworkgroup = SMB_STRDUP(myworkgroup);
if (!smb_myworkgroup)
return False;
strupper_m(smb_myworkgroup);
BOOL set_global_scope(const char *scope)
{
SAFE_FREE(smb_scope);
- smb_scope = strdup(scope);
+ smb_scope = SMB_STRDUP(scope);
if (!smb_scope)
return False;
strupper_m(smb_scope);
free_netbios_names_array();
smb_num_netbios_names = number + 1;
- smb_my_netbios_names = (char **)malloc( sizeof(char *) * smb_num_netbios_names );
+ smb_my_netbios_names = SMB_MALLOC_ARRAY( char *, smb_num_netbios_names );
if (!smb_my_netbios_names)
return False;
{
SAFE_FREE(smb_my_netbios_names[i]);
- smb_my_netbios_names[i] = strdup(name);
+ smb_my_netbios_names[i] = SMB_STRDUP(name);
if (!smb_my_netbios_names[i])
return False;
strupper_m(smb_my_netbios_names[i]);
return;
}
- *gids = Realloc(*gids, (*num+1) * sizeof(gid_t));
+ *gids = SMB_REALLOC_ARRAY(*gids, gid_t, *num+1);
if (*gids == NULL)
return;
while ((p = Atoic(p, &val, ":,")) != NULL && (*p) != ':') {
uint32 *tn;
- tn = Realloc((*num), ((*count)+1) * sizeof(uint32));
+ tn = SMB_REALLOC_ARRAY((*num), uint32, (*count)+1);
if (tn == NULL) {
SAFE_FREE(*num);
return NULL;
Make a dir struct.
****************************************************************************/
-void make_dir_struct(char *buf, const char *mask, const char *fname,SMB_OFF_T size,int mode,time_t date, BOOL case_sensitive)
+void make_dir_struct(char *buf, const char *mask, const char *fname,SMB_OFF_T size,int mode,time_t date)
{
char *p;
pstring mask2;
put_dos_date(buf,22,date);
SSVAL(buf,26,size & 0xFFFF);
SSVAL(buf,28,(size >> 16)&0xFFFF);
- push_ascii(buf+30,fname,12, case_sensitive ? 0 : STR_UPPER);
+ push_ascii(buf+30,fname,12,0);
DEBUG(8,("put name [%s] from [%s] into dir struct\n",buf+30, fname));
}
size_t num_to_read_thistime;
size_t num_written = 0;
- if ((buf = malloc(TRANSFER_BUF_SIZE)) == NULL)
+ if ((buf = SMB_MALLOC(TRANSFER_BUF_SIZE)) == NULL)
return -1;
while (total < n) {
void smb_msleep(unsigned int t)
{
+#if defined(HAVE_NANOSLEEP)
+ struct timespec tval;
+ int ret;
+
+ tval.tv_sec = t/1000;
+ tval.tv_nsec = 1000000*(t%1000);
+
+ do {
+ errno = 0;
+ ret = nanosleep(&tval, &tval);
+ } while (ret < 0 && errno == EINTR && (tval.tv_sec > 0 || tval.tv_nsec > 0));
+#else
unsigned int tdiff=0;
struct timeval tval,t1,t2;
fd_set fds;
GetTimeOfDay(&t1);
- GetTimeOfDay(&t2);
+ t2 = t1;
while (tdiff < t) {
tval.tv_sec = (t-tdiff)/1000;
tdiff = TvalDiff(&t1,&t2);
}
+#endif
}
/****************************************************************************
return(False);
}
+#if defined(PARANOID_MALLOC_CHECKER)
+
+/****************************************************************************
+ Internal malloc wrapper. Externally visible.
+****************************************************************************/
+
+void *malloc_(size_t size)
+{
+#undef malloc
+ return malloc(size);
+#define malloc(s) __ERROR_DONT_USE_MALLOC_DIRECTLY
+}
+
+/****************************************************************************
+ Internal calloc wrapper. Not externally visible.
+****************************************************************************/
+
+static void *calloc_(size_t count, size_t size)
+{
+#undef calloc
+ return calloc(count, size);
+#define calloc(n,s) __ERROR_DONT_USE_CALLOC_DIRECTLY
+}
+
+/****************************************************************************
+ Internal realloc wrapper. Not externally visible.
+****************************************************************************/
+
+static void *realloc_(void *ptr, size_t size)
+{
+#undef realloc
+ return realloc(ptr, size);
+#define realloc(p,s) __ERROR_DONT_USE_RELLOC_DIRECTLY
+}
+
+#endif /* PARANOID_MALLOC_CHECKER */
+
+/****************************************************************************
+ Type-safe malloc.
+****************************************************************************/
+
+void *malloc_array(size_t el_size, unsigned int count)
+{
+ if (count >= MAX_ALLOC_SIZE/el_size) {
+ return NULL;
+ }
+
+#if defined(PARANOID_MALLOC_CHECKER)
+ return malloc_(el_size*count);
+#else
+ return malloc(el_size*count);
+#endif
+}
+
+/****************************************************************************
+ Type-safe calloc.
+****************************************************************************/
+
+void *calloc_array(size_t size, size_t nmemb)
+{
+ if (nmemb >= MAX_ALLOC_SIZE/size) {
+ return NULL;
+ }
+#if defined(PARANOID_MALLOC_CHECKER)
+ return calloc_(nmemb, size);
+#else
+ return calloc(nmemb, size);
+#endif
+}
+
/****************************************************************************
Expand a pointer to be a particular size.
****************************************************************************/
return NULL;
}
+#if defined(PARANOID_MALLOC_CHECKER)
+ if (!p)
+ ret = (void *)malloc_(size);
+ else
+ ret = (void *)realloc_(p,size);
+#else
if (!p)
ret = (void *)malloc(size);
else
ret = (void *)realloc(p,size);
+#endif
if (!ret)
DEBUG(0,("Memory allocation error: failed to expand to %d bytes\n",(int)size));
return(ret);
}
-void *Realloc_zero(void *ptr, size_t size)
+/****************************************************************************
+ Type-safe realloc.
+****************************************************************************/
+
+void *realloc_array(void *p,size_t el_size, unsigned int count)
{
- void *tptr = NULL;
-
- tptr = Realloc(ptr, size);
- if(tptr == NULL)
+ if (count >= MAX_ALLOC_SIZE/el_size) {
return NULL;
-
- memset((char *)tptr,'\0',size);
-
- return tptr;
+ }
+ return Realloc(p,el_size*count);
}
/****************************************************************************
ZERO_ARRAY(names);
ZERO_ARRAY(namebuf);
+ /* We need to be root so we can open our /proc entry to walk
+ * our stack. It also helps when we want to dump core.
+ */
+ become_root();
+
for (i = 0; i < BACKTRACE_STACK_SIZE; i++) {
names[i] = namebuf + (i * NAMESIZE);
}
if(num_entries == 0)
return;
- if(( (*ppname_array) = (name_compare_entry *)malloc(
- (num_entries + 1) * sizeof(name_compare_entry))) == NULL) {
+ if(( (*ppname_array) = SMB_MALLOC_ARRAY(name_compare_entry, num_entries + 1)) == NULL) {
DEBUG(0,("set_namearray: malloc fail\n"));
return;
}
break;
(*ppname_array)[i].is_wild = ms_has_wild(nameptr);
- if(((*ppname_array)[i].name = strdup(nameptr)) == NULL) {
+ if(((*ppname_array)[i].name = SMB_STRDUP(nameptr)) == NULL) {
DEBUG(0,("set_namearray: malloc fail (1)\n"));
return;
}
malloc that aborts with smb_panic on fail or zero size.
*****************************************************************/
-void *smb_xmalloc(size_t size)
+void *smb_xmalloc_array(size_t size, unsigned int count)
{
void *p;
if (size == 0)
- smb_panic("smb_xmalloc: called with zero size.\n");
- if ((p = malloc(size)) == NULL) {
- DEBUG(0, ("smb_xmalloc() failed to allocate %lu bytes\n", (unsigned long)size));
- smb_panic("smb_xmalloc: malloc fail.\n");
+ smb_panic("smb_xmalloc_array: called with zero size.\n");
+ if (count >= MAX_ALLOC_SIZE/size) {
+ smb_panic("smb_xmalloc: alloc size too large.\n");
+ }
+ if ((p = SMB_MALLOC(size*count)) == NULL) {
+ DEBUG(0, ("smb_xmalloc_array failed to allocate %lu * %lu bytes\n",
+ (unsigned long)size, (unsigned long)count));
+ smb_panic("smb_xmalloc_array: malloc fail.\n");
}
return p;
}
void *smb_xmemdup(const void *p, size_t size)
{
void *p2;
- p2 = smb_xmalloc(size);
+ p2 = SMB_XMALLOC_ARRAY(unsigned char,size);
memcpy(p2, p, size);
return p2;
}
char *smb_xstrdup(const char *s)
{
+#if defined(PARANOID_MALLOC_CHECKER)
+#ifdef strdup
+#undef strdup
+#endif
+#endif
char *s1 = strdup(s);
+#if defined(PARANOID_MALLOC_CHECKER)
+#define strdup(s) __ERROR_DONT_USE_STRDUP_DIRECTLY
+#endif
if (!s1)
smb_panic("smb_xstrdup: malloc fail\n");
return s1;
+
}
/**
char *smb_xstrndup(const char *s, size_t n)
{
+#if defined(PARANOID_MALLOC_CHECKER)
+#ifdef strndup
+#undef strndup
+#endif
+#endif
char *s1 = strndup(s, n);
+#if defined(PARANOID_MALLOC_CHECKER)
+#define strndup(s,n) __ERROR_DONT_USE_STRNDUP_DIRECTLY
+#endif
if (!s1)
smb_panic("smb_xstrndup: malloc fail\n");
return s1;
void *p2;
if (size == 0)
return NULL;
- p2 = malloc(size);
+ p2 = SMB_MALLOC(size);
if (!p2)
return NULL;
memcpy(p2, p, size);