From: Amos Jeffries Date: Sun, 21 Mar 2010 03:08:26 +0000 (-0600) Subject: Compat: Shuffle squid.h portability definitions into libcompat X-Git-Tag: SQUID_3_2_0_1~345 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=dc47f5311942b1641dab0631f8bc0cd5094d8959;p=thirdparty%2Fsquid.git Compat: Shuffle squid.h portability definitions into libcompat * Improves and shuffles the memory allocator and string buffer protection * Fixing many of the unsafe string buffer handling helpers * removing several duplicated hacks already in libcompat * Adds NetBSD portability file --- diff --git a/compat/GnuRegex.c b/compat/GnuRegex.c index b5ba9eb6b5..250d336498 100644 --- a/compat/GnuRegex.c +++ b/compat/GnuRegex.c @@ -32,6 +32,7 @@ #define _GNU_SOURCE 1 #endif +#define SQUID_NO_ALLOC_PROTECT 1 #include "config.h" #if USE_GNUREGEX /* only if squid needs it. Usually not */ diff --git a/compat/Makefile.am b/compat/Makefile.am index 77c1fc31dc..4c9f8e2404 100644 --- a/compat/Makefile.am +++ b/compat/Makefile.am @@ -20,6 +20,7 @@ libcompat_la_SOURCES = \ osdetect.h \ stdvarargs.h \ types.h \ + unsafe.h \ valgrind.h \ \ os/aix.h \ @@ -28,6 +29,7 @@ libcompat_la_SOURCES = \ os/hpux.h \ os/linux.h \ os/mswin.h \ + os/netbsd.h \ os/next.h \ os/openbsd.h \ os/os2.h \ diff --git a/compat/compat.h b/compat/compat.h index a4d20698df..7260776067 100644 --- a/compat/compat.h +++ b/compat/compat.h @@ -55,6 +55,7 @@ #include "compat/os/hpux.h" #include "compat/os/linux.h" #include "compat/os/mswin.h" +#include "compat/os/netbsd.h" #include "compat/os/next.h" #include "compat/os/openbsd.h" #include "compat/os/os2.h" @@ -95,5 +96,7 @@ */ #include "compat/GnuRegex.h" +/* some functions are unsafe to be used in Squid. */ +#include "compat/unsafe.h" #endif /* _SQUID_COMPAT_H */ diff --git a/compat/compat_shared.h b/compat/compat_shared.h index 4abc979b97..95c54e7224 100644 --- a/compat/compat_shared.h +++ b/compat/compat_shared.h @@ -36,7 +36,6 @@ #if HAVE_DIRENT_H #include #define NAMLEN(dirent) strlen((dirent)->d_name) - #else /* if not HAVE_DIRENT_H */ #define dirent direct #define NAMLEN(dirent) (dirent)->d_namlen @@ -173,6 +172,7 @@ max(A const & lhs, A const & rhs) #endif +#ifndef SQUID_NONBLOCK /* REQUIRED for the below logics. If they move this needs to as well */ #if HAVE_FCNTL_H #include @@ -189,7 +189,7 @@ max(A const & lhs, A const & rhs) /** O_NDELAY is our fallback. */ #define SQUID_NONBLOCK O_NDELAY #endif - +#endif /** * Signalling flags are apparently not always provided. @@ -211,8 +211,8 @@ max(A const & lhs, A const & rhs) /** - * com_err.h is a C header and needs excplicit shielding, but not - * all other system headers including this care to do this. + * com_err.h is a C header and needs explicit shielding, but not + * all other system headers including this care to do so. */ #ifdef __cplusplus #if HAVE_ET_COM_ERR_H diff --git a/compat/os/dragonfly.h b/compat/os/dragonfly.h index e7fa90448f..5c767fc554 100644 --- a/compat/os/dragonfly.h +++ b/compat/os/dragonfly.h @@ -20,6 +20,11 @@ #undef HAVE_MALLOC_H #endif +/* Exclude CPPUnit tests from the allocator restrictions. */ +/* BSD implementation uses these still */ +#if defined(SQUID_UNIT_TEST) +#define SQUID_NO_ALLOC_PROTECT 1 +#endif #endif /* _SQUID_DRAGONFLY_ */ #endif /* SQUID_OS_DRAGONFLY_H */ diff --git a/compat/os/freebsd.h b/compat/os/freebsd.h index e4fb395942..05061fae61 100644 --- a/compat/os/freebsd.h +++ b/compat/os/freebsd.h @@ -27,5 +27,12 @@ #define _etext etext +/* Exclude CPPUnit tests from the allocator restrictions. */ +/* BSD implementation uses these still */ +#if defined(SQUID_UNIT_TEST) +#define SQUID_NO_ALLOC_PROTECT 1 +#endif + + #endif /* _SQUID_FREEBSD_ */ #endif /* SQUID_OS_FREEBSD_H */ diff --git a/compat/os/hpux.h b/compat/os/hpux.h index 993d14d4c4..25fdae1e64 100644 --- a/compat/os/hpux.h +++ b/compat/os/hpux.h @@ -27,6 +27,13 @@ #define getrusage(a, b) syscall(SYS_GETRUSAGE, a, b) #endif +/* + * getpagesize(...) not available on HPUX + */ +#if !defined(HAVE_GETPAGESIZE) +#define HAVE_GETPAGESIZE +#define getpagesize( ) sysconf(_SC_PAGE_SIZE) +#endif #endif /* _SQUID_HPUX_ */ #endif /* SQUID_OS_HPUX_H */ diff --git a/compat/os/mswin.h b/compat/os/mswin.h index 669ee7e0d1..aa90975208 100644 --- a/compat/os/mswin.h +++ b/compat/os/mswin.h @@ -716,5 +716,10 @@ struct rusage { #undef ACL +#if !defined(getpagesize) +/* Windows may lack getpagesize() prototype */ +SQUIDCEXTERN size_t getpagesize(void); +#endif + #endif /* _SQUID_WIN32_ */ #endif /* SQUID_OS_MSWIN_H */ diff --git a/compat/os/netbsd.h b/compat/os/netbsd.h new file mode 100644 index 0000000000..300f2d3646 --- /dev/null +++ b/compat/os/netbsd.h @@ -0,0 +1,23 @@ +#ifndef SQUID_CONFIG_H +#include "config.h" +#endif + +#ifndef SQUID_OS_NETBSD_H +#define SQUID_OS_NETBSD_H + +#ifdef _SQUID_NETBSD_ + +/**************************************************************************** + *--------------------------------------------------------------------------* + * DO *NOT* MAKE ANY CHANGES below here unless you know what you're doing...* + *--------------------------------------------------------------------------* + ****************************************************************************/ + +/* Exclude CPPUnit tests from the allocator restrictions. */ +/* BSD implementation uses these still */ +#if defined(SQUID_UNIT_TEST) +#define SQUID_NO_ALLOC_PROTECT 1 +#endif + +#endif /* _SQUID_NETBSD_ */ +#endif /* SQUID_OS_NETBSD_H */ diff --git a/compat/os/openbsd.h b/compat/os/openbsd.h index eae2cdf003..4f4597a147 100644 --- a/compat/os/openbsd.h +++ b/compat/os/openbsd.h @@ -20,6 +20,11 @@ #undef HAVE_MALLOC_H #endif +/* Exclude CPPUnit tests from the allocator restrictions. */ +/* BSD implementation uses these still */ +#if defined(SQUID_UNIT_TEST) +#define SQUID_NO_ALLOC_PROTECT 1 +#endif #endif /* _SQUID_OPENBSD_ */ #endif /* SQUID_OS_OPENBSD_H */ diff --git a/compat/os/sunos.h b/compat/os/sunos.h index 25f891e5da..f1c67894b9 100644 --- a/compat/os/sunos.h +++ b/compat/os/sunos.h @@ -14,6 +14,11 @@ ****************************************************************************/ +/* O_NONBLOCK requires to be included first */ +#if HAVE_FCNTL_H +#include +#endif + /* * We assume O_NONBLOCK is broken, or does not exist, on SunOS. */ diff --git a/compat/unsafe.h b/compat/unsafe.h new file mode 100644 index 0000000000..88af1fed17 --- /dev/null +++ b/compat/unsafe.h @@ -0,0 +1,33 @@ +#ifndef SQUID_CONFIG_H +#include "config.h" +#endif + +#ifndef _SQUID_COMPAT_UNSAFE_H +#define _SQUID_COMPAT_UNSAFE_H + +/* + * Trap unintentional use of functions unsafe for use within squid. + */ + +#if !SQUID_NO_ALLOC_PROTECT +#ifndef free +#define free(x) ERROR_free_UNSAFE_IN_SQUID(x) +#endif +#ifndef malloc +#define malloc ERROR_malloc_UNSAFE_IN_SQUID +#endif +#ifndef calloc +#define calloc ERROR_calloc_UNSAFE_IN_SQUID +#endif +#endif /* !SQUID_NO_ALLOC_PROTECT */ + +#if !SQUID_NO_STRING_BUFFER_PROTECT +#ifndef sprintf +#define sprintf ERROR_sprintf_UNSAFE_IN_SQUID +#endif +#ifndef strdup +#define strdup ERROR_strdup_UNSAFE_IN_SQUID +#endif +#endif /* SQUID_NO_STRING_BUFFER_PROTECT */ + +#endif /* _SQUID_COMPAT_UNSAFE_H */ diff --git a/helpers/basic_auth/MSNT/smblib.c b/helpers/basic_auth/MSNT/smblib.c index b59cc722c3..518a924a65 100644 --- a/helpers/basic_auth/MSNT/smblib.c +++ b/helpers/basic_auth/MSNT/smblib.c @@ -22,11 +22,8 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ +#define SQUID_NO_ALLOC_PROTECT 1 -int SMBlib_errno; -int SMBlib_SMB_Error; -#define SMBLIB_ERRNO -#define uchar unsigned char #include "util.h" #include "smblib-priv.h" #include "smblib.h" @@ -41,6 +38,11 @@ int SMBlib_SMB_Error; #include #include +#define SMBLIB_ERRNO +#define uchar unsigned char + +int SMBlib_errno; +int SMBlib_SMB_Error; SMB_State_Types SMBlib_State; /* Initialize the SMBlib package */ diff --git a/helpers/basic_auth/MSNT/std-includes.h b/helpers/basic_auth/MSNT/std-includes.h index 9419829392..619b45854b 100644 --- a/helpers/basic_auth/MSNT/std-includes.h +++ b/helpers/basic_auth/MSNT/std-includes.h @@ -24,12 +24,13 @@ */ /* the types are provided by squid's configure preocess */ +#define SQUID_NO_ALLOC_PROTECT 1 #include "config.h" + #define BOOL int16_t #define int16 int16_t #include -#include #include #include #include diff --git a/helpers/basic_auth/NCSA/crypt_md5.cc b/helpers/basic_auth/NCSA/crypt_md5.cc index 5823db96e0..410ac4fa04 100644 --- a/helpers/basic_auth/NCSA/crypt_md5.cc +++ b/helpers/basic_auth/NCSA/crypt_md5.cc @@ -189,7 +189,7 @@ char *md5sum(const char *s) SquidMD5Final(digest,&ctx); for (idx=0; idx<16; idx++) - sprintf(&sum[idx*2],"%02x",digest[idx]); + snprintf(&sum[idx*2],(33-(idx*2)),"%02x",digest[idx]); sum[32]='\0'; diff --git a/helpers/basic_auth/PAM/basic_pam_auth.cc b/helpers/basic_auth/PAM/basic_pam_auth.cc index dd873856cd..6a2a31abf6 100644 --- a/helpers/basic_auth/PAM/basic_pam_auth.cc +++ b/helpers/basic_auth/PAM/basic_pam_auth.cc @@ -65,6 +65,8 @@ * * Compile this program with: gcc -o basic_pam_auth basic_pam_auth.cc -lpam -ldl */ +#define SQUID_NO_ALLOC_PROTECT 1 +#define SQUID_NO_STRING_BUFFER_PROTECT 1 #include "config.h" #include "rfc1738.h" diff --git a/helpers/digest_auth/password/text_backend.c b/helpers/digest_auth/password/text_backend.c index 9bbaf1d479..a934bbb11c 100644 --- a/helpers/digest_auth/password/text_backend.c +++ b/helpers/digest_auth/password/text_backend.c @@ -28,6 +28,8 @@ * * Copyright (c) 2003 Robert Collins */ +#define SQUID_NO_ALLOC_PROTECT 1 +#include "config.h" #include "text_backend.h" diff --git a/helpers/negotiate_auth/kerberos/negotiate_kerberos_auth.cc b/helpers/negotiate_auth/kerberos/negotiate_kerberos_auth.cc index 68f2a78974..1f575736a2 100644 --- a/helpers/negotiate_auth/kerberos/negotiate_kerberos_auth.cc +++ b/helpers/negotiate_auth/kerberos/negotiate_kerberos_auth.cc @@ -171,7 +171,7 @@ check_gss_err(OM_uint32 major_status, OM_uint32 minor_status, GSS_C_GSS_CODE, GSS_C_NULL_OID, &msg_ctx, &status_string); if (maj_stat == GSS_S_COMPLETE) { if (sizeof(buf) > len + status_string.length + 1) { - sprintf(buf + len, "%s", (char *) status_string.value); + snprintf(buf + len, (sizeof(buf)-len), "%s", (char *) status_string.value); len += status_string.length; } gss_release_buffer(&min_stat, &status_string); @@ -180,7 +180,7 @@ check_gss_err(OM_uint32 major_status, OM_uint32 minor_status, gss_release_buffer(&min_stat, &status_string); } if (sizeof(buf) > len + 2) { - sprintf(buf + len, "%s", ". "); + snprintf(buf + len, (sizeof(buf)-len), "%s", ". "); len += 2; } msg_ctx = 0; @@ -190,7 +190,7 @@ check_gss_err(OM_uint32 major_status, OM_uint32 minor_status, GSS_C_MECH_CODE, GSS_C_NULL_OID, &msg_ctx, &status_string); if (maj_stat == GSS_S_COMPLETE) { if (sizeof(buf) > len + status_string.length) { - sprintf(buf + len, "%s", (char *) status_string.value); + snprintf(buf + len,(sizeof(buf)-len), "%s", (char *) status_string.value); len += status_string.length; } gss_release_buffer(&min_stat, &status_string); diff --git a/helpers/negotiate_auth/kerberos/negotiate_kerberos_auth_test.cc b/helpers/negotiate_auth/kerberos/negotiate_kerberos_auth_test.cc index 90ea8067b6..f15ebb515c 100644 --- a/helpers/negotiate_auth/kerberos/negotiate_kerberos_auth_test.cc +++ b/helpers/negotiate_auth/kerberos/negotiate_kerberos_auth_test.cc @@ -122,7 +122,7 @@ check_gss_err(OM_uint32 major_status, OM_uint32 minor_status, GSS_C_GSS_CODE, GSS_C_NULL_OID, &msg_ctx, &status_string); if (maj_stat == GSS_S_COMPLETE) { if (sizeof(buf) > len + status_string.length + 1) { - sprintf(buf + len, "%s", (char *) status_string.value); + snprintf(buf + len, (sizeof(buf)-len), "%s", (char *)status_string.value); len += status_string.length; } gss_release_buffer(&min_stat, &status_string); @@ -131,7 +131,7 @@ check_gss_err(OM_uint32 major_status, OM_uint32 minor_status, gss_release_buffer(&min_stat, &status_string); } if (sizeof(buf) > len + 2) { - sprintf(buf + len, "%s", ". "); + snprintf(buf + len, (sizeof(buf)-len), "%s", ". "); len += 2; } msg_ctx = 0; @@ -141,7 +141,7 @@ check_gss_err(OM_uint32 major_status, OM_uint32 minor_status, GSS_C_MECH_CODE, GSS_C_NULL_OID, &msg_ctx, &status_string); if (maj_stat == GSS_S_COMPLETE) { if (sizeof(buf) > len + status_string.length) { - sprintf(buf + len, "%s", (char *) status_string.value); + snprintf(buf + len, (sizeof(buf)-len), "%s", (char *) status_string.value); len += status_string.length; } gss_release_buffer(&min_stat, &status_string); diff --git a/helpers/ntlm_auth/smb_lm/ntlm_smb_lm_auth.c b/helpers/ntlm_auth/smb_lm/ntlm_smb_lm_auth.c index 597150bf8f..5c0acba56a 100644 --- a/helpers/ntlm_auth/smb_lm/ntlm_smb_lm_auth.c +++ b/helpers/ntlm_auth/smb_lm/ntlm_smb_lm_auth.c @@ -13,11 +13,11 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. * */ - +#define SQUID_NO_ALLOC_PROTECT 1 #include "config.h" + #include "ntlmauth.h" #include "ntlm_smb_lm_auth.h" -#include "squid_endian.h" #include "util.h" #include "smbval/smblib-common.h" #include "smbval/rfcnb-error.h" diff --git a/helpers/ntlm_auth/smb_lm/smbval/byteorder.h b/helpers/ntlm_auth/smb_lm/smbval/byteorder.h index de91c4574f..138bcfe9d9 100644 --- a/helpers/ntlm_auth/smb_lm/smbval/byteorder.h +++ b/helpers/ntlm_auth/smb_lm/smbval/byteorder.h @@ -46,12 +46,12 @@ #define IVAL(buf,pos) (SVAL(buf,pos)|SVAL(buf,(pos)+2)<<16) #define SSVALX(buf,pos,val) (CVAL(buf,pos)=(val)&0xFF,CVAL(buf,pos+1)=(val)>>8) #define SIVALX(buf,pos,val) (SSVALX(buf,pos,val&0xFFFF),SSVALX(buf,pos+2,val>>16)) -#define SVALS(buf,pos) ((int16)SVAL(buf,pos)) -#define IVALS(buf,pos) ((int32)IVAL(buf,pos)) -#define SSVAL(buf,pos,val) SSVALX((buf),(pos),((uint16)(val))) -#define SIVAL(buf,pos,val) SIVALX((buf),(pos),((uint32)(val))) -#define SSVALS(buf,pos,val) SSVALX((buf),(pos),((int16)(val))) -#define SIVALS(buf,pos,val) SIVALX((buf),(pos),((int32)(val))) +#define SVALS(buf,pos) ((int16_t)SVAL(buf,pos)) +#define IVALS(buf,pos) ((int32_t)IVAL(buf,pos)) +#define SSVAL(buf,pos,val) SSVALX((buf),(pos),((uint16_t)(val))) +#define SIVAL(buf,pos,val) SIVALX((buf),(pos),((uint32_t)(val))) +#define SSVALS(buf,pos,val) SSVALX((buf),(pos),((int16_t)(val))) +#define SIVALS(buf,pos,val) SIVALX((buf),(pos),((int32_t)(val))) #else /* this handles things for architectures like the 386 that can handle * alignment errors */ @@ -59,14 +59,14 @@ * WARNING: This section is dependent on the length of int16 and int32 * being correct */ -#define SVAL(buf,pos) (*(uint16 *)((char *)(buf) + (pos))) -#define IVAL(buf,pos) (*(uint32 *)((char *)(buf) + (pos))) -#define SVALS(buf,pos) (*(int16 *)((char *)(buf) + (pos))) -#define IVALS(buf,pos) (*(int32 *)((char *)(buf) + (pos))) -#define SSVAL(buf,pos,val) SVAL(buf,pos)=((uint16)(val)) -#define SIVAL(buf,pos,val) IVAL(buf,pos)=((uint32)(val)) -#define SSVALS(buf,pos,val) SVALS(buf,pos)=((int16)(val)) -#define SIVALS(buf,pos,val) IVALS(buf,pos)=((int32)(val)) +#define SVAL(buf,pos) (*(uint16_t *)((char *)(buf) + (pos))) +#define IVAL(buf,pos) (*(uint32_t *)((char *)(buf) + (pos))) +#define SVALS(buf,pos) (*(int16_t *)((char *)(buf) + (pos))) +#define IVALS(buf,pos) (*(int32_t *)((char *)(buf) + (pos))) +#define SSVAL(buf,pos,val) SVAL(buf,pos)=((uint16_t)(val)) +#define SIVAL(buf,pos,val) IVAL(buf,pos)=((uint32_t)(val)) +#define SSVALS(buf,pos,val) SVALS(buf,pos)=((int16_t)(val)) +#define SIVALS(buf,pos,val) IVALS(buf,pos)=((int32_t)(val)) #endif diff --git a/helpers/ntlm_auth/smb_lm/smbval/rfcnb-util.c b/helpers/ntlm_auth/smb_lm/smbval/rfcnb-util.c index 50a6ce870d..20165dc52d 100644 --- a/helpers/ntlm_auth/smb_lm/smbval/rfcnb-util.c +++ b/helpers/ntlm_auth/smb_lm/smbval/rfcnb-util.c @@ -22,9 +22,10 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ +#define SQUID_NO_ALLOC_PROTECT 1 +#include "config.h" #include -#include #include "std-includes.h" #include "rfcnb-priv.h" @@ -32,14 +33,6 @@ #include "rfcnb-io.h" #include -#ifndef uint16 -#ifdef u_int16_t -typedef u_int16_t uint16; -#else -typedef unsigned short uint16; -#endif -#endif - extern void (*Prot_Print_Routine) (); /* Pointer to protocol print routine */ /* Convert name and pad to 16 chars as needed */ diff --git a/helpers/ntlm_auth/smb_lm/smbval/session.c b/helpers/ntlm_auth/smb_lm/smbval/session.c index 65776a3602..f434683603 100644 --- a/helpers/ntlm_auth/smb_lm/smbval/session.c +++ b/helpers/ntlm_auth/smb_lm/smbval/session.c @@ -22,9 +22,10 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ +#define SQUID_NO_ALLOC_PROTECT 1 +#include "config.h" #include -#include int RFCNB_errno = 0; int RFCNB_saved_errno = 0; @@ -347,14 +348,12 @@ RFCNB_Listen() void RFCNB_Get_Error(char *buffer, int buf_len) { - if (RFCNB_saved_errno <= 0) { - sprintf(buffer, "%s", RFCNB_Error_Strings[RFCNB_errno]); + snprintf(buffer, buf_len, "%s", RFCNB_Error_Strings[RFCNB_errno]); } else { - sprintf(buffer, "%s\n\terrno:%s", RFCNB_Error_Strings[RFCNB_errno], + snprintf(buffer, buf_len, "%s\n\terrno:%s", RFCNB_Error_Strings[RFCNB_errno], strerror(RFCNB_saved_errno)); } - } /* Pick up the last error response and returns as a code */ diff --git a/helpers/ntlm_auth/smb_lm/smbval/smblib-util.c b/helpers/ntlm_auth/smb_lm/smbval/smblib-util.c index b25814b1e0..c49110902a 100644 --- a/helpers/ntlm_auth/smb_lm/smbval/smblib-util.c +++ b/helpers/ntlm_auth/smb_lm/smbval/smblib-util.c @@ -22,9 +22,10 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ +#define SQUID_NO_ALLOC_PROTECT 1 +#include "config.h" #include "smblib-priv.h" -#include #include #include "rfcnb.h" @@ -112,7 +113,7 @@ SMB_DOSTimToStr(int DOS_time) DOS_month = (DOS_time & 0x01E00000) >> 21; DOS_year = ((DOS_time & 0xFE000000) >> 25) + 80; - sprintf(SMB_Time_Temp, "%2d/%02d/%2d %2d:%02d:%02d", DOS_day, DOS_month, + snprintf(SMB_Time_Temp, 48, "%2d/%02d/%2d %2d:%02d:%02d", DOS_day, DOS_month, DOS_year, DOS_hour, DOS_min, DOS_sec); return (SMB_Time_Temp); diff --git a/helpers/ntlm_auth/smb_lm/smbval/smblib.c b/helpers/ntlm_auth/smb_lm/smbval/smblib.c index 1e06998c9f..ae1dfe59de 100644 --- a/helpers/ntlm_auth/smb_lm/smbval/smblib.c +++ b/helpers/ntlm_auth/smb_lm/smbval/smblib.c @@ -23,9 +23,9 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ - +#define SQUID_NO_ALLOC_PROTECT 1 #include "config.h" -#include + #include #include diff --git a/include/MemPool.h b/include/MemPool.h index 4abd92dac6..b51ce23ac1 100644 --- a/include/MemPool.h +++ b/include/MemPool.h @@ -209,7 +209,7 @@ public: /** * Free a element allocated by MemAllocator::alloc() */ - virtual void free(void *) = 0; + virtual void freeOne(void *) = 0; virtual char const *objectType() const; virtual size_t objectSize() const = 0; @@ -259,7 +259,7 @@ public: /** * Free a element allocated by MemAllocatorProxy::alloc() */ - void free(void *); + void freeOne(void *); int inUseCount() const; size_t objectSize() const; @@ -317,7 +317,7 @@ CLASS::operator new (size_t byteCount) \ void \ CLASS::operator delete (void *address) \ { \ - Pool().free(address); \ + Pool().freeOne(address); \ } /// \ingroup MemPoolsAPI @@ -338,7 +338,7 @@ public: /** * Free a element allocated by MemImplementingAllocator::alloc() */ - virtual void free(void *); + virtual void freeOne(void *); virtual bool idleTrigger(int shift) const = 0; virtual void clean(time_t maxage) = 0; diff --git a/lib/MemPool.cc b/lib/MemPool.cc index 7150d4b900..c09b897633 100644 --- a/lib/MemPool.cc +++ b/lib/MemPool.cc @@ -555,7 +555,7 @@ MemImplementingAllocator::alloc() } void -MemImplementingAllocator::free(void *obj) +MemImplementingAllocator::freeOne(void *obj) { assert(obj != NULL); (void) VALGRIND_CHECK_MEM_IS_ADDRESSABLE(obj, obj_size); @@ -882,9 +882,9 @@ MemAllocatorProxy::alloc() } void -MemAllocatorProxy::free(void *address) +MemAllocatorProxy::freeOne(void *address) { - getAllocator()->free(address); + getAllocator()->freeOne(address); /* TODO: check for empty, and if so, if the default type has altered, * switch */ diff --git a/lib/Profiler.c b/lib/Profiler.c index 0e5b7aec5b..d026a3ce58 100644 --- a/lib/Profiler.c +++ b/lib/Profiler.c @@ -1,4 +1,3 @@ - /* * $Id$ * @@ -33,13 +32,15 @@ * */ -/* +/** * CPU Profiling implementation. * + * \par * This library implements the Probes needed to gather stats. * See src/ProfStats.c which implements historical recording and * presentation in CacheMgr.cgi. * + * \par * For timing we prefer on-CPU ops that retrieve cpu ticks counter. * For Intel, this is "rdtsc", which is 64-bit counter that virtually * never wraps. For alpha, this is "rpcc" which is 32-bit counter and @@ -56,16 +57,19 @@ * probename must be added to profiling.h into xprof_type enum list * with prepended "XPROF_" string. * - * Description. - * PROF gathers stats per probename into structures. It indexes these + * \section description Description. + * \par PROF + * gathers stats per probename into structures. It indexes these * structures by enum type index in an array. * - * PROF records best, best, average and worst values for delta time, + * \par PROF + * records best, best, average and worst values for delta time, * also, if UNACCED is defined, it measures "empty" time during which * no probes are in measuring state. This allows to see time "unaccounted" * for. If OVERHEAD is defined, additional calculations are made at every * probe to measure approximate overhead of the probe code itself. * + * \par * Probe data is stored in linked-list, so the more probes you define, * the more overhead is added to find the deepest nested probe. To reduce * average overhead, linked list is manipulated each time PR_start is @@ -77,17 +81,20 @@ * fast and frequent sections of code, we want to reduce this overhead * to absolute minimum possible. * + * \par * For actual measurements, probe overhead cancels out mostly. Still, * do not take the measured times as facts, they should be viewed in * relative comparison to overall CPU time and on the same platform. * + * \par * Every 1 second, Event within squid is called that parses gathered * statistics of every probe, and accumulates that into historical * structures for last 1,5,30 secs, 1,5,30 mins, and 1,5 and 24 hours. * Each second active probe stats are reset, and only historical data * is presented in cachemgr output. * - * Reading stats. + * \section reading Reading stats. + * \par * "Worst case" may be misleading. Anything can happen at any section * of code that could delay reaching to probe stop. For eg. system may * need to service interrupt routine, task switch could occur, or page @@ -99,19 +106,23 @@ * in given section of code, and its average completion time. This data * could be used to detect bottlenecks withing squid and optimise them. * + * \par * TOTALS are quite off reality. Its there just to summarise cumulative * times and percent column. Percent values over 100% shows that there * have been some probes nested into each other. * */ -#include "profiling.h" - -#include +#define SQUID_NO_ALLOC_PROTECT 1 +#include "config.h" #ifdef USE_XPROF_STATS +#include "profiling.h" +#if HAVE_ASSERT_H +#include +#endif #if HAVE_GNUMALLLOC_H #include #elif HAVE_MALLOC_H diff --git a/lib/util.c b/lib/util.c index e856c2e795..0e75acec50 100644 --- a/lib/util.c +++ b/lib/util.c @@ -1,4 +1,3 @@ - /* * $Id$ * @@ -35,6 +34,9 @@ #define _etext etext +/* NP: this file is where the memory allocators are defined. */ +#define SQUID_NO_ALLOC_PROTECT 1 + #include "config.h" #include "profiling.h" diff --git a/snmplib/mib.c b/snmplib/mib.c index f4b1337975..777e807f9c 100644 --- a/snmplib/mib.c +++ b/snmplib/mib.c @@ -21,6 +21,7 @@ ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ******************************************************************/ +#define SQUID_NO_STRING_BUFFER_PROTECT 1 #include "config.h" #include diff --git a/src/DiskIO/DiskThreads/DiskThreadsIOStrategy.cc b/src/DiskIO/DiskThreads/DiskThreadsIOStrategy.cc index d97be3a7eb..d2cc0fd6ee 100644 --- a/src/DiskIO/DiskThreads/DiskThreadsIOStrategy.cc +++ b/src/DiskIO/DiskThreads/DiskThreadsIOStrategy.cc @@ -166,7 +166,7 @@ DiskThreadsIOStrategy::callback() if (ctrlp->operation == _AIO_READ) squidaio_xfree(ctrlp->bufp, ctrlp->len); - squidaio_ctrl_pool->free(ctrlp); + squidaio_ctrl_pool->freeOne(ctrlp); } return retval; diff --git a/src/DiskIO/DiskThreads/aiops.cc b/src/DiskIO/DiskThreads/aiops.cc index 6008e7b9f0..ca753f7e32 100644 --- a/src/DiskIO/DiskThreads/aiops.cc +++ b/src/DiskIO/DiskThreads/aiops.cc @@ -218,7 +218,7 @@ squidaio_xfree(void *p, int size) MemAllocator *pool; if ((pool = squidaio_get_pool(size)) != NULL) { - pool->free(p); + pool->freeOne(p); } else xfree(p); } @@ -230,7 +230,7 @@ squidaio_xstrfree(char *str) int len = strlen(str) + 1; if ((pool = squidaio_get_pool(len)) != NULL) { - pool->free(str); + pool->freeOne(str); } else xfree(str); } @@ -634,7 +634,7 @@ squidaio_cleanup_request(squidaio_request_t * requestp) resultp->aio_errno = requestp->err; } - squidaio_request_pool->free(requestp); + squidaio_request_pool->freeOne(requestp); } /* squidaio_cleanup_request */ diff --git a/src/DiskIO/DiskThreads/async_io.cc b/src/DiskIO/DiskThreads/async_io.cc index d0113b468a..f5a6f1c0bc 100644 --- a/src/DiskIO/DiskThreads/async_io.cc +++ b/src/DiskIO/DiskThreads/async_io.cc @@ -125,7 +125,7 @@ aioCancel(int fd) } dlinkDelete(m, &used_list); - DiskThreadsIOStrategy::Instance.squidaio_ctrl_pool->free(ctrlp); + DiskThreadsIOStrategy::Instance.squidaio_ctrl_pool->freeOne(ctrlp); } } diff --git a/src/acl/Strategised.h b/src/acl/Strategised.h index 9f4494e076..b222e975ad 100644 --- a/src/acl/Strategised.h +++ b/src/acl/Strategised.h @@ -99,7 +99,7 @@ template void ACLStrategised::operator delete (void *address) { - Pool->free(address); + Pool->freeOne(address); } template diff --git a/src/auth/digest/auth_digest.cc b/src/auth/digest/auth_digest.cc index a884b62500..5006ca3b32 100644 --- a/src/auth/digest/auth_digest.cc +++ b/src/auth/digest/auth_digest.cc @@ -220,7 +220,7 @@ authenticateDigestNonceDelete(digest_nonce_h * nonce) safe_free(nonce->key); - digest_nonce_pool->free(nonce); + digest_nonce_pool->freeOne(nonce); } } diff --git a/src/cbdata.cc b/src/cbdata.cc index ab5ee3a6e8..e9515d87fd 100644 --- a/src/cbdata.cc +++ b/src/cbdata.cc @@ -379,10 +379,10 @@ cbdataInternalFree(void *p) #if HASHED_CBDATA hash_remove_link(cbdata_htable, &c->hash); delete c; - cbdata_index[theType].pool->free((void *)p); + cbdata_index[theType].pool->freeOne((void *)p); #else c->cbdata::~cbdata(); - cbdata_index[theType].pool->free(c); + cbdata_index[theType].pool->freeOne(c); #endif return NULL; } @@ -489,10 +489,10 @@ cbdataInternalUnlock(const void *p) #if HASHED_CBDATA hash_remove_link(cbdata_htable, &c->hash); delete c; - cbdata_index[theType].pool->free((void *)p); + cbdata_index[theType].pool->freeOne((void *)p); #else c->cbdata::~cbdata(); - cbdata_index[theType].pool->free(c); + cbdata_index[theType].pool->freeOne(c); #endif } diff --git a/src/comm_poll.cc b/src/comm_poll.cc index 9be098844e..cec07ac256 100644 --- a/src/comm_poll.cc +++ b/src/comm_poll.cc @@ -1,4 +1,3 @@ - /* * $Id$ * @@ -41,6 +40,20 @@ #ifdef USE_POLL +#if HAVE_POLL_H +#include +#endif + +/* Needed for poll() on Linux at least */ +#if USE_POLL +#ifndef POLLRDNORM +#define POLLRDNORM POLLIN +#endif +#ifndef POLLWRNORM +#define POLLWRNORM POLLOUT +#endif +#endif + static int MAX_POLL_TIME = 1000; /* see also comm_quick_poll_required() */ #ifndef howmany diff --git a/src/dlink.cc b/src/dlink.cc index 3296dd1063..6094bb6124 100644 --- a/src/dlink.cc +++ b/src/dlink.cc @@ -31,7 +31,7 @@ dlinkNodeDelete(dlink_node * m) if (m == NULL) return; - dlink_node_pool->free(m); + dlink_node_pool->freeOne(m); } void diff --git a/src/helper.cc b/src/helper.cc index b7ff5dc6a6..000cfc8ad4 100644 --- a/src/helper.cc +++ b/src/helper.cc @@ -786,7 +786,7 @@ helperStatefulServerFree(int fd, void *data) } if (srv->data != NULL) - hlp->datapool->free(srv->data); + hlp->datapool->freeOne(srv->data); cbdataReferenceDone(srv->parent); diff --git a/src/htcp.cc b/src/htcp.cc index b3c269d915..f947f065d2 100644 --- a/src/htcp.cc +++ b/src/htcp.cc @@ -639,7 +639,7 @@ htcpFreeSpecifier(htcpSpecifier * s) static void htcpFreeDetail(htcpDetail * d) { - htcpDetailPool->free(d); + htcpDetailPool->freeOne(d); } /* diff --git a/src/mem.cc b/src/mem.cc index 226712b880..5e197e94f7 100644 --- a/src/mem.cc +++ b/src/mem.cc @@ -186,7 +186,7 @@ memAllocate(mem_type type) void memFree(void *p, int type) { - MemPools[type]->free(p); + MemPools[type]->freeOne(p); } /* allocate a variable size buffer using best-fit pool */ @@ -241,7 +241,7 @@ memFreeString(size_t size, void *buf) memMeterDec(StrCountMeter); memMeterDel(StrVolumeMeter, size); - pool ? pool->free(buf) : xfree(buf); + pool ? pool->freeOne(buf) : xfree(buf); } /* Find the best fit MEM_X_BUF type */ diff --git a/src/pconn.cc b/src/pconn.cc index 571d798699..0b89563fa1 100644 --- a/src/pconn.cc +++ b/src/pconn.cc @@ -61,7 +61,7 @@ IdleConnList::~IdleConnList() parent->unlinkList(this); if (nfds_alloc == PCONN_FDS_SZ) - pconn_fds_pool->free(fds); + pconn_fds_pool->freeOne(fds); else xfree(fds); @@ -118,7 +118,7 @@ IdleConnList::push(int fd) xmemcpy(fds, old, nfds * sizeof(int)); if (nfds == PCONN_FDS_SZ) - pconn_fds_pool->free(old); + pconn_fds_pool->freeOne(old); else xfree(old); } diff --git a/src/repl/lru/store_repl_lru.cc b/src/repl/lru/store_repl_lru.cc index b62c4f012a..893a5a6059 100644 --- a/src/repl/lru/store_repl_lru.cc +++ b/src/repl/lru/store_repl_lru.cc @@ -135,7 +135,7 @@ lru_remove(RemovalPolicy * policy, StoreEntry * entry, RemovalPolicyNode * node) dlinkDelete(&lru_node->node, &lru->list); - lru_node_pool->free(lru_node); + lru_node_pool->freeOne(lru_node); lru->count -= 1; } @@ -249,7 +249,7 @@ try_again: goto try_again; } - lru_node_pool->free(lru_node); + lru_node_pool->freeOne(lru_node); lru->count -= 1; lru->setPolicyNode(entry, NULL); return entry; diff --git a/src/squid.h b/src/squid.h index 7c6a3ab576..09e1d34924 100644 --- a/src/squid.h +++ b/src/squid.h @@ -52,9 +52,6 @@ using namespace Squid; #if HAVE_ERRNO_H #include #endif -#if HAVE_FCNTL_H -#include -#endif #if HAVE_GRP_H #include #endif @@ -132,114 +129,17 @@ using namespace Squid; #ifdef _SQUID_WIN32_ #include #endif - -#if HAVE_DIRENT_H -#include -#define NAMLEN(dirent) strlen((dirent)->d_name) -#else /* HAVE_DIRENT_H */ -#define dirent direct -#define NAMLEN(dirent) (dirent)->d_namlen -#if HAVE_SYS_NDIR_H -#include -#endif /* HAVE_SYS_NDIR_H */ -#if HAVE_SYS_DIR_H -#include -#endif /* HAVE_SYS_DIR_H */ -#if HAVE_NDIR_H -#include -#endif /* HAVE_NDIR_H */ -#endif /* HAVE_DIRENT_H */ - -#if defined(__QNX__) -#include -#endif - #if HAVE_SYS_MOUNT_H #include #endif - -/* - * We require poll.h before using poll(). If the symbols used - * by poll() are defined elsewhere, we will need to make this - * a more sophisticated test. - * -- Oskar Pearson - * -- Stewart Forster - */ -#if USE_POLL -#if HAVE_POLL_H -#include -#endif /* HAVE_POLL_H */ -#endif /* USE_POLL */ - - -/* - * Trap unintentional use of fd_set. Must not be used outside the - * select code as it only supports FD_SETSIZE number of filedescriptors - * and Squid may be running with a lot more.. - * But only for code linked into Squid, not the helpers.. (unlinkd, pinger) - */ -#ifdef SQUID_FDSET_NOUSE -# ifndef SQUID_HELPER -# define fd_set ERROR_FD_SET_USED -# endif -#endif - #if HAVE_MATH_H #include #endif -#if 0 // moved to include/rfc2181.h - RFC defined constants -#define SQUIDHOSTNAMELEN 256 -#endif - #ifndef MAXPATHLEN #define MAXPATHLEN SQUID_MAXPATHLEN #endif - -#if !HAVE_STRUCT_RUSAGE -/* - * If we don't have getrusage() then we create a fake structure - * with only the fields Squid cares about. This just makes the - * source code cleaner, so we don't need lots of #ifdefs in other - * places - */ - -struct rusage { - - struct timeval ru_stime; - - struct timeval ru_utime; - int ru_maxrss; - int ru_majflt; -}; - -#endif - -#if !defined(HAVE_GETPAGESIZE) && defined(_SQUID_HPUX_) -#define HAVE_GETPAGESIZE -#define getpagesize( ) sysconf(_SC_PAGE_SIZE) -#endif - -#if defined(_SQUID_MSWIN_) && !defined(getpagesize) -/* Windows may lack getpagesize() prototype */ -SQUIDCEXTERN size_t getpagesize(void); -#endif /* _SQUID_MSWIN_ */ - -#ifndef SA_RESTART -#define SA_RESTART 0 -#endif -#ifndef SA_NODEFER -#define SA_NODEFER 0 -#endif -#ifndef SA_RESETHAND -#define SA_RESETHAND 0 -#endif -#if SA_RESETHAND == 0 && defined(SA_ONESHOT) -#undef SA_RESETHAND -#define SA_RESETHAND SA_ONESHOT -#endif - #if LEAK_CHECK_MODE #define LOCAL_ARRAY(type,name,size) \ static type *local_##name=NULL; \ @@ -259,16 +159,6 @@ SQUIDCEXTERN size_t getpagesize(void); #include "ssl_support.h" #endif -/* Needed for poll() on Linux at least */ -#if USE_POLL -#ifndef POLLRDNORM -#define POLLRDNORM POLLIN -#endif -#ifndef POLLWRNORM -#define POLLWRNORM POLLOUT -#endif -#endif - #ifdef SQUID_SNMP #include "cache_snmp.h" #endif @@ -306,55 +196,6 @@ SQUIDCEXTERN size_t getpagesize(void); #include "protos.h" #include "globals.h" -/* Exclude CPPUnit tests from the below restriction. */ -/* BSD implementation uses these still */ -#if !defined(SQUID_UNIT_TEST) -/* - * Squid source files should not call these functions directly. - * Use xmalloc, xfree, xcalloc, snprintf, and xstrdup instead. - * Also use xmemcpy, xisspace, ... - */ -#ifndef malloc -#define malloc + -#endif -template -void free(V x) { fatal("Do not use ::free()"); } -#ifndef calloc -#define calloc + -#endif -#ifndef sprintf -#define sprintf + -#endif -#ifndef strdup -#define strdup + -#endif -#endif /* !SQUID_UNIT_TEST */ - -/* - * Hey dummy, don't be tempted to move this to lib/config.h.in - * again. O_NONBLOCK will not be defined there because you didn't - * #include yet. - */ -#if defined(_SQUID_SUNOS_) -/* - * We assume O_NONBLOCK is broken, or does not exist, on SunOS. - */ -#define SQUID_NONBLOCK O_NDELAY -#elif defined(O_NONBLOCK) -/* -* We used to assume O_NONBLOCK was broken on Solaris, but evidence -* now indicates that its fine on Solaris 8, and in fact required for -* properly detecting EOF on FIFOs. So now we assume that if -* its defined, it works correctly on all operating systems. -*/ -#define SQUID_NONBLOCK O_NONBLOCK -/* -* O_NDELAY is our fallback. -*/ -#else -#define SQUID_NONBLOCK O_NDELAY -#endif - /* * I'm sick of having to keep doing this .. */ diff --git a/src/store.cc b/src/store.cc index 69b93059dc..f0f51d053c 100644 --- a/src/store.cc +++ b/src/store.cc @@ -169,7 +169,7 @@ StoreEntry::operator new (size_t bytecount) void StoreEntry::operator delete (void *address) { - pool->free(address); + pool->freeOne(address); } void diff --git a/test-suite/MemPoolTest.cc b/test-suite/MemPoolTest.cc index fdb6f79b85..f44177e305 100644 --- a/test-suite/MemPoolTest.cc +++ b/test-suite/MemPoolTest.cc @@ -74,11 +74,11 @@ MemPoolTest::run() assert (something); assert (something->aValue == 0); something->aValue = 5; - Pool->free(something); + Pool->freeOne(something); SomethingToAlloc *otherthing = static_cast(Pool->alloc()); assert (otherthing == something); assert (otherthing->aValue == 0); - Pool->free (otherthing); + Pool->freeOne(otherthing); delete Pool; } diff --git a/test-suite/test_tools.cc b/test-suite/test_tools.cc index a2a93c1bd8..0f6754acfe 100644 --- a/test-suite/test_tools.cc +++ b/test-suite/test_tools.cc @@ -170,7 +170,7 @@ dlinkNodeDelete(dlink_node * m) if (m == NULL) return; - dlink_node_pool->free(m); + dlink_node_pool->freeOne(m); } void diff --git a/tools/squidclient.cc b/tools/squidclient.cc index a7fe5005b6..e2a82ca330 100644 --- a/tools/squidclient.cc +++ b/tools/squidclient.cc @@ -353,7 +353,7 @@ main(int argc, char *argv[]) if (newhost) { char *t; newhost += 3; - newhost = strdup(newhost); + newhost = xstrdup(newhost); t = newhost + strcspn(newhost, "@/?"); if (*t == '@') { newhost = t + 1;