[ntp:bk-ntp-stable-send] BitKeeper diffs

Harlan Stenn stenn at whimsy.udel.edu
Thu Apr 10 06:42:20 UTC 2008


#### ChangeSet ####
2008-03-24 22:12:42-04:00, mayer at pogo.udel.edu 
  bugs 993 a d 959

==== ChangeLog ====
2008-03-24 22:12:26-04:00, mayer at pogo.udel.edu +2 -0
  bugs 993 a d 959

--- 1.37/ChangeLog	2008-03-23 02:14:50 -05:00
+++ 1.38/ChangeLog	2008-03-24 21:12:26 -05:00
@@ -1,3 +1,5 @@
+* [Bug 959] Refclock on Windows not properly releasing recvbuffs.
+* [Bug 993] Fix memory leak when fetching system messages.
 * Correctly apply negative-sawtooth for oncore 12 channel receiver.
 * Startup code for original LinuxPPS removed.  LinuxPPS now conforms to
   the PPSAPI.

#### ChangeSet ####
2008-03-24 21:41:55-04:00, mayer at pogo.udel.edu 
  [Bug 959] Refclock on Windows not properly releasing recvbuffs

==== ports/winnt/ntpd/ntp_iocompletionport.c ====
2008-03-24 21:41:37-04:00, mayer at pogo.udel.edu +25 -13
  [Bug 959] Refclock on Windows not properly releasing recvbuffs

--- 1.24/ports/winnt/ntpd/ntp_iocompletionport.c	2008-02-18 09:30:31 -05:00
+++ 1.25/ports/winnt/ntpd/ntp_iocompletionport.c	2008-03-24 20:41:37 -05:00
@@ -16,6 +16,8 @@
 #include "ntp_refclock.h"
 #include "ntp_iocompletionport.h"
 #include "transmitbuff.h"
+#include "ntp_request.h"
+#include "ntp_io.h"
 
 /*
  * Request types
@@ -49,9 +51,6 @@ static int OnSocketRecv(DWORD, IoComplet
 static int OnIoReadComplete(DWORD, IoCompletionInfo *, DWORD, int);
 static int OnWriteComplete(DWORD, IoCompletionInfo *, DWORD, int);
 
-
-#define BUFCHECK_SECS	10
-static void	TransmitCheckThread(void *NotUsed);
 static HANDLE hHeapHandle = NULL;
 
 static HANDLE hIoCompletionPort = NULL;
@@ -294,14 +293,11 @@ uninit_io_completion_port(
 
 static int QueueIORead( struct refclockio *rio, recvbuf_t *buff, IoCompletionInfo *lpo) {
 
-	memset(lpo, 0, sizeof(IoCompletionInfo));
-	memset(buff, 0, sizeof(recvbuf_t));
-
 	lpo->request_type = CLOCK_READ;
 	lpo->recv_buf = buff;
 
 	buff->fd = rio->fd;
-	if (!ReadFile((HANDLE) buff->fd, &buff->recv_buffer, sizeof(buff->recv_buffer), NULL, (LPOVERLAPPED) lpo)) {
+	if (!ReadFile((HANDLE) buff->fd, buff->wsabuff.buf, buff->wsabuff.len, NULL, (LPOVERLAPPED) lpo)) {
 		DWORD Result = GetLastError();
 		switch (Result) {				
 		case NO_ERROR :
@@ -330,6 +326,9 @@ OnIoReadComplete(DWORD i, IoCompletionIn
 	recvbuf_t *buff;
 	recvbuf_t *newbuff;
 	struct refclockio * rio = (struct refclockio *) i;
+	l_fp arrival_time;
+
+	get_systime(&arrival_time);
 
 	/*
 	 * Get the recvbuf pointer from the overlapped buffer.
@@ -352,7 +351,7 @@ OnIoReadComplete(DWORD i, IoCompletionIn
 		 * ignore 0 bytes read due to timeout's and closure on fd
 		 */
 		if (Bytes > 0 && errstatus != WSA_OPERATION_ABORTED) {
-			get_systime(&buff->recv_time);
+			memcpy(&buff->recv_time, &arrival_time, sizeof(arrival_time));	
 			buff->recv_length = (int) Bytes;
 			buff->receiver = rio->clock_recv;
 			buff->dstadr = NULL;
@@ -470,8 +469,12 @@ OnSocketRecv(DWORD i, IoCompletionInfo *
 {
 	struct recvbuf *buff = NULL;
 	recvbuf_t *newbuff;
+	isc_boolean_t ignore_this;
+	l_fp arrival_time;
 	struct interface * inter = (struct interface *) i;
 	
+	get_systime(&arrival_time);	
+
 	/*  Convert the overlapped pointer back to a recvbuf pointer.
 	*/
 	
@@ -486,7 +489,6 @@ OnSocketRecv(DWORD i, IoCompletionInfo *
 	 * Make sure we have a buffer
 	 */
 	if (buff == NULL) {
-//		FreeHeap(lpo, "OnSocketRecv: Socket Closed");
 		return (1);
 	}
 
@@ -515,17 +517,27 @@ OnSocketRecv(DWORD i, IoCompletionInfo *
 	}
 	else 
 	{
+#ifdef DEBUG
+		if(debug > 3 && get_packet_mode(buff) == MODE_BROADCAST)
+			printf("****Accepting Broadcast packet on fd %d from %s\n", buff->fd, stoa(&buff->recv_srcadr));
+#endif
+		ignore_this = inter->ignore_packets;
+#ifdef DEBUG
+		if (debug > 3)
+			printf(" Packet mode is %d\n", get_packet_mode(buff));
+#endif
+
 		/*
 		 * If we keep it add some info to the structure
 		 */
-		if (Bytes > 0 && inter->ignore_packets == ISC_FALSE) {
-			get_systime(&buff->recv_time);	
+		if (Bytes > 0 && ignore_this == ISC_FALSE) {
+			memcpy(&buff->recv_time, &arrival_time, sizeof(arrival_time));	
 			buff->recv_length = (int) Bytes;
 			buff->receiver = receive; 
 			buff->dstadr = inter;
 #ifdef DEBUG
-			if (debug > 3)
-  				printf("Received %d bytes in buffer %x from %s\n", Bytes, buff, stoa(&buff->recv_srcadr));
+			if (debug > 1)
+  				printf("Received %d bytes of fd %d in buffer %x from %s\n", Bytes, buff->fd, buff, stoa(&buff->recv_srcadr));
 #endif
 			add_full_recv_buffer(buff);
 			/*

#### ChangeSet ####
2008-03-24 21:40:32-04:00, mayer at pogo.udel.edu 
  [Bug 993] Windows: Fix memory leak when fetching system messages

==== ports/winnt/libisc/isc_strerror.c ====
2008-03-24 21:40:07-04:00, mayer at pogo.udel.edu +78 -18
  [Bug 993] Windows: Fix memory leak when fetching system messages

--- 1.4/ports/winnt/libisc/isc_strerror.c	2004-11-02 23:07:08 -05:00
+++ 1.5/ports/winnt/libisc/isc_strerror.c	2008-03-24 20:40:07 -05:00
@@ -17,9 +17,35 @@
 
 /* From BIND 9 lib/isc/win32/: strerror.c,v 1.5 2002/08/01 03:52:14 mayer */
 
+/*
+ * We don't need this warning message
+ */
+#pragma warning(disable: 4127) /* conditional expression is constant */
+
 #include <stdio.h>
 #include <string.h>
 #include <windows.h>
+#include <isc/list.h>
+
+
+/*
+ * Messsage list
+ */
+typedef struct msg_list msg_list_t;
+
+struct msg_list {
+	int code;
+	char *msg;
+	ISC_LINK(msg_list_t) link;
+};
+
+static ISC_LIST(msg_list_t) errormsg_list;
+
+BOOL initialized = FALSE;
+
+static CRITICAL_SECTION ErrorMsgLock;
+# define LOCK(lock)	EnterCriticalSection(lock)
+# define UNLOCK(lock)	LeaveCriticalSection(lock)
 
 /*
  * Forward declarations
@@ -32,12 +58,24 @@ char *
 GetWSAErrorMessage(int errval);
 
 static char *
-isc__NTstrerror(int err, BOOL *bfreebuf);
+isc__NTstrerror(int err);
+
+/*
+ * Initialize the error message list
+ */
+
+void
+initialize() {
+	ISC_LIST_INIT(errormsg_list);
+	InitializeCriticalSection(&ErrorMsgLock);
+	initialized = TRUE;
+}
 
 char *
 NTstrerror(int errnum) {
-	BOOL bfreebuf;
-	return (isc__NTstrerror(errnum, &bfreebuf));
+	if(!initialized)
+		initialize();
+	return (isc__NTstrerror(errnum));
 }
 /*
  * This routine needs to free up any buffer allocated by FormatMessage
@@ -47,18 +85,15 @@ NTstrerror(int errnum) {
 void
 isc__strerror(int num, char *buf, size_t size) {
 	char *msg;
-	BOOL freebuf;
 	unsigned int unum = num;
+	if(!initialized)
+		initialize();
 
-	freebuf = FALSE;
-	msg = isc__NTstrerror(num, &freebuf);
+	msg = isc__NTstrerror(num);
 	if (msg != NULL)
 		_snprintf(buf, size, "%s", msg);
 	else
 		_snprintf(buf, size, "Unknown error: %u", unum);
-	if(freebuf && msg != NULL) {
-		LocalFree(msg);
-	}
 }
 
 /*
@@ -70,6 +105,29 @@ isc__strerror(int num, char *buf, size_t
 char *
 FormatError(int error) {
 	LPVOID lpMsgBuf = NULL;
+
+	msg_list_t *lmsg; 
+
+	/*
+	 * See if we already have the error code
+	 */
+
+	LOCK(&ErrorMsgLock);
+
+	lmsg = ISC_LIST_HEAD(errormsg_list);
+	while (lmsg != NULL) {
+		if (lmsg->code == error) {
+			lpMsgBuf = lmsg->msg;
+			UNLOCK(&ErrorMsgLock);
+			return (lpMsgBuf);
+		}
+		lmsg = ISC_LIST_NEXT(lmsg, link);
+	}
+
+	/*
+	 * Not found
+	 */
+
 	FormatMessage( 
 		FORMAT_MESSAGE_ALLOCATE_BUFFER | 
 		FORMAT_MESSAGE_FROM_SYSTEM | 
@@ -82,6 +140,13 @@ FormatError(int error) {
 		0,
 		NULL); 
 
+	if (lpMsgBuf != NULL) {
+		lmsg = malloc(sizeof(msg_list_t));
+		lmsg->code = error;
+		lmsg->msg = lpMsgBuf;
+		ISC_LIST_APPEND(errormsg_list, lmsg, link);
+	}
+	UNLOCK(&ErrorMsgLock);
 	return (lpMsgBuf);
 }
 
@@ -91,14 +156,12 @@ FormatError(int error) {
  * since those messages are not available in the system error messages.
  */
 static char *
-isc__NTstrerror(int err, BOOL *bfreebuf) {
+isc__NTstrerror(int err) {
 	char *retmsg = NULL;
 
 	/* Copy the error value first in case of other errors */	
 	DWORD errval = err; 
 
-	*bfreebuf = FALSE;
-
 	/* Get the Winsock2 error messages */
 	if (errval >= WSABASEERR && errval <= (WSABASEERR + 1015)) {
 		retmsg = GetWSAErrorMessage(errval);
@@ -110,7 +173,6 @@ isc__NTstrerror(int err, BOOL *bfreebuf)
 	 * try a system error message
 	 */
 	if (errval > (DWORD) _sys_nerr) {
-		*bfreebuf = TRUE;
 		return (FormatError(errval));
 	} else {
 		return (_sys_errlist[errval]);
@@ -124,14 +186,12 @@ void __cdecl
 NTperror(char *errmsg) {
 	/* Copy the error value first in case of other errors */
 	int errval = errno; 
-	BOOL bfreebuf = FALSE;
 	char *msg;
+	if(!initialized)
+		initialize();
 
-	msg = isc__NTstrerror(errval, &bfreebuf);
+	msg = isc__NTstrerror(errval);
 	fprintf(stderr, "%s: %s\n", errmsg, msg);
-	if(bfreebuf == TRUE) {
-		LocalFree(msg);
-	}
 
 }
 


More information about the bk-ntp-stable-send mailing list