summaryrefslogtreecommitdiffstats
path: root/libntp/emalloc.c
diff options
context:
space:
mode:
authorroberto <roberto@FreeBSD.org>2013-12-04 21:33:17 +0000
committerroberto <roberto@FreeBSD.org>2013-12-04 21:33:17 +0000
commitd54cfbdce4a9878ef65216dea36b62cf6646b84b (patch)
treea618007bb41d13153794a598e3d904ace2976324 /libntp/emalloc.c
parentfd23eea016bd30c806a3ee90eb6f397470c2fa46 (diff)
downloadFreeBSD-src-d54cfbdce4a9878ef65216dea36b62cf6646b84b.zip
FreeBSD-src-d54cfbdce4a9878ef65216dea36b62cf6646b84b.tar.gz
Virgin import of ntpd 4.2.6p5.
When the series of commits is complete, things like https://cert.litnet.lt/en/docs/ntp-distributed-reflection-dos-attacks should be fixed. PR: bin/148836 (except that we import a newer version) Asked by: Too many MFC after: 2 weeks
Diffstat (limited to 'libntp/emalloc.c')
-rw-r--r--libntp/emalloc.c90
1 files changed, 69 insertions, 21 deletions
diff --git a/libntp/emalloc.c b/libntp/emalloc.c
index 2fe4c38..c49c5c1 100644
--- a/libntp/emalloc.c
+++ b/libntp/emalloc.c
@@ -1,48 +1,96 @@
/*
* emalloc - return new memory obtained from the system. Belch if none.
*/
+#include <config.h>
#include "ntp_types.h"
#include "ntp_malloc.h"
#include "ntp_syslog.h"
#include "ntp_stdlib.h"
-#if defined SYS_WINNT && defined DEBUG
-#include <crtdbg.h>
-#endif
-#if defined SYS_WINNT && defined DEBUG
+/*
+ * When using the debug MS CRT allocator, each allocation stores the
+ * callsite __FILE__ and __LINE__, which is then displayed at process
+ * termination, to track down leaks. We don't want all of our
+ * allocations to show up as coming from emalloc.c, so we preserve the
+ * original callsite's source file and line using macros which pass
+ * __FILE__ and __LINE__ as parameters to these routines.
+ * Other debug malloc implementations can be used by defining
+ * EREALLOC_IMPL() as ports/winnt/include/config.h does.
+ */
void *
-debug_emalloc(
- u_int size,
- char *filename,
- int line
+ereallocz(
+ void * ptr,
+ size_t newsz,
+ size_t priorsz,
+ int zero_init
+#ifdef EREALLOC_CALLSITE /* ntp_malloc.h */
+ ,
+ const char * file,
+ int line
+#endif
)
{
- char *mem;
+ char * mem;
+ size_t allocsz;
+
+ if (0 == newsz)
+ allocsz = 1;
+ else
+ allocsz = newsz;
- if ((mem = (char *)_malloc_dbg(size, _NORMAL_BLOCK, filename, line)) == 0) {
- msyslog(LOG_ERR, "Exiting: No more memory!");
+ mem = EREALLOC_IMPL(ptr, allocsz, file, line);
+ if (NULL == mem) {
+ msyslog_term = TRUE;
+#ifndef EREALLOC_CALLSITE
+ msyslog(LOG_ERR, "fatal out of memory (%lu bytes)",
+ (u_long)newsz);
+#else
+ msyslog(LOG_ERR,
+ "fatal out of memory %s line %d (%lu bytes)",
+ file, line, (u_long)newsz);
+#endif
exit(1);
}
+
+ if (zero_init && newsz > priorsz)
+ zero_mem(mem + priorsz, newsz - priorsz);
+
return mem;
}
-#else
-void *
-emalloc(
- u_int size
+char *
+estrdup_impl(
+ const char * str
+#ifdef EREALLOC_CALLSITE
+ ,
+ const char * file,
+ int line
+#endif
)
{
- char *mem;
+ char * copy;
+ size_t bytes;
- if ((mem = (char *)malloc(size)) == 0) {
- msyslog(LOG_ERR, "Exiting: No more memory!");
- exit(1);
- }
- return mem;
+ bytes = strlen(str) + 1;
+ copy = ereallocz(NULL, bytes, 0, FALSE
+#ifdef EREALLOC_CALLSITE
+ , file, line
+#endif
+ );
+ memcpy(copy, str, bytes);
+
+ return copy;
}
+#ifndef EREALLOC_CALLSITE
+void *
+emalloc(size_t newsz)
+{
+ return ereallocz(NULL, newsz, 0, FALSE);
+}
#endif
+
OpenPOWER on IntegriCloud