From e59797e3bda39abf611063fc0ba38fcb4e6596e4 Mon Sep 17 00:00:00 2001 From: Jeremy Harris Date: Sun, 16 Jun 2019 18:10:59 +0100 Subject: [PATCH] Inline the smaller string-handling functions --- src/OS/Makefile-Base | 12 ++-- src/exim_monitor/em_hdr.h | 6 +- src/exim_monitor/em_version.c | 1 + src/src/acl.c | 2 +- src/src/arc.c | 8 +-- src/src/dkim.c | 2 +- src/src/functions.h | 132 ++++++++++++++++++++++++++++++++-- src/src/local_scan.h | 14 +++- src/src/lookups/ldap.c | 2 +- src/src/mytypes.h | 1 + src/src/string.c | 105 ++++----------------------- 11 files changed, 171 insertions(+), 114 deletions(-) diff --git a/src/OS/Makefile-Base b/src/OS/Makefile-Base index 8ca9a485c..f8c6ebb53 100644 --- a/src/OS/Makefile-Base +++ b/src/OS/Makefile-Base @@ -623,11 +623,11 @@ OBJ_MONBIN = util-spool_in.o \ util-string.o \ util-queue.o \ util-tod.o \ - tree.o \ + util-tree.o \ $(MONBIN) -eximon.bin: $(EXIMON_EDITME) eximon $(OBJ_MONBIN) \ - ../exim_monitor/em_version.c +eximon.bin: $(EXIMON_EDITME) eximon $(OBJ_MONBIN) ../exim_monitor/em_version.c \ + mytypes.h store.h macros.h @echo "$(CC) exim_monitor/em_version.c" $(FE)$(CC) -o em_version.o -c \ $(CFLAGS) $(XINCLUDE) -I. ../exim_monitor/em_version.c @@ -757,6 +757,10 @@ util-tod.o: $(HDRS) tod.c @echo "$(CC) -DCOMPILE_UTILITY tod.c" $(FE)$(CC) -c $(CFLAGS) $(INCLUDE) -DCOMPILE_UTILITY -o util-tod.o tod.c +util-tree.o: $(HDRS) tree.c + @echo "$(CC) -DCOMPILE_UTILITY tree.c" + $(FE)$(CC) -c $(CFLAGS) $(INCLUDE) -DCOMPILE_UTILITY -o util-tree.o tree.c + util-os.o: $(HDRS) os.c @echo "$(CC) -DCOMPILE_UTILITY os.c" $(FE)$(CC) -c $(CFLAGS) $(INCLUDE) \ @@ -770,7 +774,7 @@ util-os.o: $(HDRS) os.c local_scan.o: config local_scan.h ../$(LOCAL_SCAN_SOURCE) @echo "$(CC) local_scan.c" - $(FE)$(CC) -c $(CFLAGS) -I. $(INCLUDE) -o local_scan.o ../$(LOCAL_SCAN_SOURCE) + $(FE)$(CC) -DLOCAL_SCAN -c $(CFLAGS) -I. $(INCLUDE) -o local_scan.o ../$(LOCAL_SCAN_SOURCE) # Dependencies for the "ordinary" exim modules diff --git a/src/exim_monitor/em_hdr.h b/src/exim_monitor/em_hdr.h index ada9f36b4..c45f9fca7 100644 --- a/src/exim_monitor/em_hdr.h +++ b/src/exim_monitor/em_hdr.h @@ -95,18 +95,14 @@ this interface so that this kind of kludge isn't needed. */ #endif typedef void hctx; -#include "config.h" -#include "mytypes.h" -#include "macros.h" - #include "local_scan.h" +#include "macros.h" #include "structs.h" #include "blob.h" #include "globals.h" #include "dbstuff.h" #include "functions.h" #include "osfunctions.h" -#include "store.h" /* The sys/resource.h header on SunOS 4 causes trouble with the gcc compiler. Just stuff the bit we want in here; pragmatic easy way out. */ diff --git a/src/exim_monitor/em_version.c b/src/exim_monitor/em_version.c index ff9ac5c58..e5a4ebbb0 100644 --- a/src/exim_monitor/em_version.c +++ b/src/exim_monitor/em_version.c @@ -6,6 +6,7 @@ /* See the file NOTICE for conditions of use and distribution. */ #include "mytypes.h" +#include "store.h" #include "macros.h" #include #include diff --git a/src/src/acl.c b/src/src/acl.c index 19938affa..3788612f8 100644 --- a/src/src/acl.c +++ b/src/src/acl.c @@ -2171,7 +2171,7 @@ va_start(ap, format); g = string_vformat(g, TRUE, format, ap); va_end(ap); -gstring_reset_unused(g); +gstring_release_unused(g); *log_msgptr = string_from_gstring(g); return ERROR; } diff --git a/src/src/arc.c b/src/src/arc.c index e7ebb9719..7bdf4bfbb 100644 --- a/src/src/arc.c +++ b/src/src/arc.c @@ -263,7 +263,7 @@ while ((c = *s)) if (!g) return US"no b= value"; al->b.data = string_from_gstring(g); al->b.len = g->ptr; - gstring_reset_unused(g); + gstring_release_unused(g); bend = s; break; case 'h': /* bh= AMS body hash */ @@ -280,7 +280,7 @@ while ((c = *s)) if (!g) return US"no bh= value"; al->bh.data = string_from_gstring(g); al->bh.len = g->ptr; - gstring_reset_unused(g); + gstring_release_unused(g); break; default: return US"b? tag"; @@ -1282,7 +1282,7 @@ for (;;) g = string_catn(g, US"\r\n\t ", 5); } g = string_catn(g, US";\r\n", 3); -gstring_reset_unused(g); +gstring_release_unused(g); string_from_gstring(g); return g; } @@ -1700,7 +1700,7 @@ if (g) if (sigheaders) g = string_catn(g, sigheaders->s, sigheaders->ptr); (void) string_from_gstring(g); -gstring_reset_unused(g); +gstring_release_unused(g); return g; } diff --git a/src/src/dkim.c b/src/src/dkim.c index 96d7eba81..715774c7e 100644 --- a/src/src/dkim.c +++ b/src/src/dkim.c @@ -76,7 +76,7 @@ for (dns_record * rr = dns_next_rr(&dnsa, &dnss, RESET_ANSWERS); /* check if this looks like a DKIM record */ if (Ustrncmp(g->s, "v=", 2) != 0 || strncasecmp(CS g->s, "v=dkim", 6) == 0) { - gstring_reset_unused(g); + gstring_release_unused(g); return string_from_gstring(g); } diff --git a/src/src/functions.h b/src/src/functions.h index 7b5b00a2d..1273e8138 100644 --- a/src/src/functions.h +++ b/src/src/functions.h @@ -240,8 +240,6 @@ extern BOOL filter_system_interpret(address_item **, uschar **); extern uschar * fn_hdrs_added(void); -extern void gstring_reset_unused(gstring *); - extern void header_add(int, const char *, ...); extern header_line *header_add_at_position_internal(BOOL, uschar *, BOOL, int, const char *, ...); extern int header_checkname(header_line *, BOOL); @@ -497,14 +495,10 @@ extern gstring *string_catn(gstring *, const uschar *, int) WARN_UNUSED_RESULT; extern int string_compare_by_pointer(const void *, const void *); extern uschar *string_copy_dnsdomain(uschar *); extern uschar *string_copy_malloc(const uschar *); -extern uschar *string_copylc(const uschar *); -extern uschar *string_copynlc(uschar *, int); extern uschar *string_dequote(const uschar **); extern gstring *string_fmt_append(gstring *, const char *, ...) ALMOST_PRINTF(2,3); extern BOOL string_format(uschar *, int, const char *, ...) ALMOST_PRINTF(3,4); extern uschar *string_format_size(int, uschar *); -extern uschar *string_from_gstring(gstring *); -extern gstring *string_get(unsigned); extern int string_interpret_escape(const uschar **); extern int string_is_ip_address(const uschar *, int *); #ifdef SUPPORT_I18N @@ -623,6 +617,132 @@ exim_chown(const uschar *name, uid_t owner, gid_t group) return chown(CCS name, owner, group) ? exim_chown_failure(-1, name, owner, group) : 0; } + +#endif /* !MACRO_PREDEF && !COMPILE_UTILITY */ +/******************************************************************************/ +/* String functions */ + +/************************************************* +* Copy and save string * +*************************************************/ + +/* This function assumes that memcpy() is faster than strcpy(). +*/ + +#if !defined(MACRO_PREDEF) +static inline uschar * +string_copy(const uschar *s) +{ +int len = Ustrlen(s) + 1; +uschar *ss = store_get(len); +memcpy(ss, s, len); +return ss; +} + + +/************************************************* +* Copy, lowercase and save string * +*************************************************/ + +/* +Argument: string to copy +Returns: copy of string in new store, with letters lowercased +*/ + +static inline uschar * +string_copylc(const uschar *s) +{ +uschar *ss = store_get(Ustrlen(s) + 1); +uschar *p = ss; +while (*s != 0) *p++ = tolower(*s++); +*p = 0; +return ss; +} + + + +/************************************************* +* Copy and save string, given length * +*************************************************/ + +/* It is assumed the data contains no zeros. A zero is added +onto the end. + +Arguments: + s string to copy + n number of characters + +Returns: copy of string in new store + +This is an API for local_scan hence not static. +*/ + +static inline uschar * +string_copyn(const uschar *s, int n) +{ +uschar *ss = store_get(n + 1); +Ustrncpy(ss, s, n); +ss[n] = 0; +return ss; +} + +/************************************************* +* Copy, lowercase, and save string, given length * +*************************************************/ + +/* It is assumed the data contains no zeros. A zero is added +onto the end. + +Arguments: + s string to copy + n number of characters + +Returns: copy of string in new store, with letters lowercased +*/ + +static inline uschar * +string_copynlc(uschar *s, int n) +{ +uschar *ss = store_get(n + 1); +uschar *p = ss; +while (n-- > 0) *p++ = tolower(*s++); +*p = 0; +return ss; +} + + +/******************************************************************************/ +/* Growable-string functions */ + +/* Create a growable-string with some preassigned space, in untainted memory */ + +static inline gstring * +string_get(unsigned size) +{ +gstring * g = store_get(sizeof(gstring) + size); +g->size = size; +g->ptr = 0; +g->s = US(g + 1); +return g; +} + +/* NUL-terminate the C string in the growable-string, and return it. */ + +static inline uschar * +string_from_gstring(gstring * g) +{ +if (!g) return NULL; +g->s[g->ptr] = '\0'; +return g->s; +} + +static inline void +gstring_release_unused(gstring * g) +{ +if (g) store_reset(g->s + (g->size = g->ptr + 1)); +} + +/******************************************************************************/ #endif /* !MACRO_PREDEF */ #endif /* _FUNCTIONS_H_ */ diff --git a/src/src/local_scan.h b/src/src/local_scan.h index c974ac6e7..dced8bd52 100644 --- a/src/src/local_scan.h +++ b/src/src/local_scan.h @@ -188,8 +188,18 @@ extern uschar *rfc2047_decode(uschar *, BOOL, uschar *, int, int *, uschar **); extern int smtp_fflush(void); extern void smtp_printf(const char *, BOOL, ...) PRINTF_FUNCTION(1,3); extern void smtp_vprintf(const char *, BOOL, va_list); -extern uschar *string_copy(const uschar *); -extern uschar *string_copyn(const uschar *, int); extern uschar *string_sprintf(const char *, ...) ALMOST_PRINTF(1,2); +#ifdef LOCAL_SCAN +/* When compiling a local_scan() file we want to rename a published API, so that +we can use an inlined implementation in the compiles of the main Exim files, +with the original name. */ + +# define string_copy(s) string_copy_function(s) +# define string_copyn(s, n) string_copyn_function(s, n) + +extern uschar * string_copy_function(const uschar *); +extern uschar * string_copyn_function(const uschar *); +#endif + /* End of local_scan.h */ diff --git a/src/src/lookups/ldap.c b/src/src/lookups/ldap.c index f77318626..08f68b75e 100644 --- a/src/src/lookups/ldap.c +++ b/src/src/lookups/ldap.c @@ -838,7 +838,7 @@ an empty string. */ if (!data) data = string_get(1); (void) string_from_gstring(data); -gstring_reset_unused(data); +gstring_release_unused(data); /* Copy the last dn into eldap_dn */ diff --git a/src/src/mytypes.h b/src/src/mytypes.h index 4234574c9..5871bddfc 100644 --- a/src/src/mytypes.h +++ b/src/src/mytypes.h @@ -114,6 +114,7 @@ functions that are called quite often; for other calls to external libraries #define Ustrtol(s,t,b) strtol(CCS(s),CSS(t),b) #define Ustrtoul(s,t,b) strtoul(CCS(s),CSS(t),b) #define Uunlink(s) unlink(CCS(s)) + #endif /* End of mytypes.h */ diff --git a/src/src/string.c b/src/src/string.c index 739e05fb5..fed210080 100644 --- a/src/src/string.c +++ b/src/src/string.c @@ -407,6 +407,7 @@ return ss; +#ifdef HAVE_LOCAL_SCAN /************************************************* * Copy and save string * *************************************************/ @@ -418,7 +419,7 @@ Returns: copy of string in new store */ uschar * -string_copy(const uschar *s) +string_copy_function(const uschar *s) { int len = Ustrlen(s) + 1; uschar *ss = store_get(len); @@ -427,49 +428,6 @@ return ss; } - -/************************************************* -* Copy and save string in malloc'd store * -*************************************************/ - -/* This function assumes that memcpy() is faster than strcpy(). - -Argument: string to copy -Returns: copy of string in new store -*/ - -uschar * -string_copy_malloc(const uschar *s) -{ -int len = Ustrlen(s) + 1; -uschar *ss = store_malloc(len); -memcpy(ss, s, len); -return ss; -} - - - -/************************************************* -* Copy, lowercase and save string * -*************************************************/ - -/* -Argument: string to copy -Returns: copy of string in new store, with letters lowercased -*/ - -uschar * -string_copylc(const uschar *s) -{ -uschar *ss = store_get(Ustrlen(s) + 1); -uschar *p = ss; -while (*s != 0) *p++ = tolower(*s++); -*p = 0; -return ss; -} - - - /************************************************* * Copy and save string, given length * *************************************************/ @@ -485,36 +443,32 @@ Returns: copy of string in new store */ uschar * -string_copyn(const uschar *s, int n) +string_copyn_function(const uschar *s, int n) { uschar *ss = store_get(n + 1); Ustrncpy(ss, s, n); ss[n] = 0; return ss; } +#endif /************************************************* -* Copy, lowercase, and save string, given length * +* Copy and save string in malloc'd store * *************************************************/ -/* It is assumed the data contains no zeros. A zero is added -onto the end. - -Arguments: - s string to copy - n number of characters +/* This function assumes that memcpy() is faster than strcpy(). -Returns: copy of string in new store, with letters lowercased +Argument: string to copy +Returns: copy of string in new store */ uschar * -string_copynlc(uschar *s, int n) +string_copy_malloc(const uschar *s) { -uschar *ss = store_get(n + 1); -uschar *p = ss; -while (n-- > 0) *p++ = tolower(*s++); -*p = 0; +int len = Ustrlen(s) + 1; +uschar *ss = store_malloc(len); +memcpy(ss, s, len); return ss; } @@ -736,7 +690,7 @@ if (!gp2) #ifdef COMPILE_UTILITY return string_copy(gp->s); #else -gstring_reset_unused(gp); +gstring_release_unused(gp); return gp->s; #endif } @@ -983,7 +937,7 @@ else } while (g->ptr > 0 && isspace(g->s[g->ptr-1])) g->ptr--; buffer = string_from_gstring(g); - gstring_reset_unused(g); + gstring_release_unused(g); } /* Update the current pointer and return the new string */ @@ -1093,35 +1047,6 @@ return list; /************************************************/ -/* Create a growable-string with some preassigned space */ - -gstring * -string_get(unsigned size) -{ -gstring * g = store_get(sizeof(gstring) + size); -g->size = size; -g->ptr = 0; -g->s = US(g + 1); -return g; -} - -/* NUL-terminate the C string in the growable-string, and return it. */ - -uschar * -string_from_gstring(gstring * g) -{ -if (!g) return NULL; -g->s[g->ptr] = '\0'; -return g->s; -} - -void -gstring_reset_unused(gstring * g) -{ -store_reset(g->s + (g->size = g->ptr + 1)); -} - - /* Add more space to a growable-string. Arguments: @@ -1662,7 +1587,7 @@ doesn't seem much we can do about that. */ va_start(ap, format); (void) string_vformat(g, FALSE, format, ap); string_from_gstring(g); -gstring_reset_unused(g); +gstring_release_unused(g); va_end(ap); return eno == EACCES -- 2.25.1