Explorar el Código

two memory problems solved, one more unitialized value left somewhere in the Read service

Stasik0 hace 10 años
padre
commit
f6d2ec0fca
Se han modificado 4 ficheros con 35 adiciones y 40 borrados
  1. 9 5
      examples/networklayer_tcp.c
  2. 22 20
      src/server/ua_server.c
  3. 1 1
      src/server/ua_server_binary.c
  4. 3 14
      src/ua_types.c

+ 9 - 5
examples/networklayer_tcp.c

@@ -25,6 +25,7 @@
 #include <stdio.h>
 #include <errno.h> // errno, EINTR
 #include <fcntl.h> // fcntl
+#include <string.h> // memset
 
 #include "networklayer_tcp.h" // UA_MULTITHREADING is defined in here
 
@@ -218,6 +219,7 @@ void writeCallback(TCPConnection *handle, UA_ByteStringArray gather_buf) {
 	UA_UInt32 total_len = 0, nWritten = 0;
 #ifdef _WIN32
 	LPWSABUF buf = _alloca(gather_buf.stringsSize * sizeof(WSABUF));
+	memset(buf, 0, sizeof(gather_buf.stringsSize * sizeof(WSABUF)));
 	int result = 0;
 	for(UA_UInt32 i = 0; i<gather_buf.stringsSize; i++) {
 		buf[i].buf = (char*)gather_buf.strings[i].data;
@@ -236,14 +238,16 @@ void writeCallback(TCPConnection *handle, UA_ByteStringArray gather_buf) {
 	}
 #else
 	struct iovec iov[gather_buf.stringsSize];
+	memset(iov, 0, sizeof(struct iovec)*gather_buf.stringsSize);
 	for(UA_UInt32 i=0;i<gather_buf.stringsSize;i++) {
-		iov[i] = (struct iovec) {.iov_base = gather_buf.strings[i].data,
-                                 .iov_len = gather_buf.strings[i].length};
+		iov[i].iov_base = gather_buf.strings[i].data;
+		iov[i].iov_len = gather_buf.strings[i].length;
 		total_len += gather_buf.strings[i].length;
 	}
-	struct msghdr message = {.msg_name = NULL, .msg_namelen = 0, .msg_iov = iov,
-							 .msg_iovlen = gather_buf.stringsSize, .msg_control = NULL,
-							 .msg_controllen = 0, .msg_flags = 0};
+	struct msghdr message;
+	memset(&message, 0, sizeof(message));
+	message.msg_iov = iov;
+	message.msg_iovlen = gather_buf.stringsSize;
 	while (nWritten < total_len) {
 		UA_Int32 n = 0;
 		do {

+ 22 - 20
src/server/ua_server.c

@@ -57,29 +57,31 @@ void UA_Server_setLogger(UA_Server *server, UA_Logger logger) {
 /**********/
 
 void UA_Server_delete(UA_Server *server) {
-    // The server needs to be stopped before it can be deleted
+	// The server needs to be stopped before it can be deleted
+
+	// Delete all internal data
+	UA_ApplicationDescription_deleteMembers(&server->description);
+	UA_SecureChannelManager_deleteMembers(&server->secureChannelManager);
+	UA_SessionManager_deleteMembers(&server->sessionManager);
+	UA_NodeStore_delete(server->nodestore);
+	UA_ByteString_deleteMembers(&server->serverCertificate);
+	UA_Array_delete(server->endpointDescriptions, &UA_TYPES[UA_TYPES_ENDPOINTDESCRIPTION], server->endpointDescriptionsSize);
+
+	// Delete the timed work
+	UA_Server_deleteTimedWork(server);
+
+
+	// Delete the network layers
+	for(UA_Int32 i=0;i<server->nlsSize;i++) {
+		server->nls[i].free(server->nls[i].nlHandle);
+	}
+	UA_free(server->nls);
 
-    // Delete the network layers
-    for(UA_Int32 i=0;i<server->nlsSize;i++) {
-        server->nls[i].free(server->nls[i].nlHandle);
-    }
-    UA_free(server->nls);
-
-    // Delete the timed work
-    UA_Server_deleteTimedWork(server);
-
-    // Delete all internal data
-    UA_ApplicationDescription_deleteMembers(&server->description);
-    UA_SecureChannelManager_deleteMembers(&server->secureChannelManager);
-    UA_SessionManager_deleteMembers(&server->sessionManager);
-    UA_NodeStore_delete(server->nodestore);
-    UA_ByteString_deleteMembers(&server->serverCertificate);
-    UA_Array_delete(server->endpointDescriptions, &UA_TYPES[UA_TYPES_ENDPOINTDESCRIPTION], server->endpointDescriptionsSize);
 #ifdef UA_MULTITHREADING
-    pthread_cond_destroy(&server->dispatchQueue_condition); // so the workers don't spin if the queue is empty
-    rcu_barrier(); // wait for all scheduled call_rcu work to complete
+	pthread_cond_destroy(&server->dispatchQueue_condition); // so the workers don't spin if the queue is empty
+	rcu_barrier(); // wait for all scheduled call_rcu work to complete
 #endif
-    UA_free(server);
+	UA_free(server);
 }
 
 static UA_StatusCode readStatus(const void *handle, UA_Boolean sourceTimeStamp, UA_DataValue *value) {

+ 1 - 1
src/server/ua_server_binary.c

@@ -140,7 +140,7 @@ static void init_response_header(const UA_RequestHeader *p, UA_ResponseHeader *r
             messageOnStack = UA_TRUE;                                   \
             *MESSAGE = (UA_ByteString){.length = messageSize,           \
                                        .data = UA_alloca(messageSize)}; \
-                        } else                                                          \
+                        } else                                          \
             UA_ByteString_newMembers(MESSAGE, messageSize);             \
     } while(0)
 

+ 3 - 14
src/ua_types.c

@@ -540,7 +540,7 @@ void UA_DataValue_deleteMembers(UA_DataValue *p) {
 }
 
 void UA_DataValue_init(UA_DataValue *p) {
-    *((UA_Byte*)p) = 0; // zero out the bitfield 
+    *((UA_Byte*)p) = 0; // zero out the bitfield
     p->serverPicoseconds = 0;
     UA_DateTime_init(&p->serverTimestamp);
     p->sourcePicoseconds = 0;
@@ -662,13 +662,7 @@ void UA_DiagnosticInfo_deleteMembers(UA_DiagnosticInfo *p) {
 
 UA_TYPE_NEW_DEFAULT(UA_DiagnosticInfo)
 void UA_DiagnosticInfo_init(UA_DiagnosticInfo *p) {
-    p->hasSymbolicId          = UA_FALSE;
-    p->hasNamespaceUri        = UA_FALSE;
-    p->hasLocalizedText       = UA_FALSE;
-    p->hasLocale              = UA_FALSE;
-    p->hasAdditionalInfo      = UA_FALSE;
-    p->hasInnerStatusCode     = UA_FALSE;
-    p->hasInnerDiagnosticInfo = UA_FALSE;
+	*((UA_Byte*)p) = 0; // zero out the bitfield
     p->symbolicId          = 0;
     p->namespaceUri        = 0;
     p->localizedText       = 0;
@@ -680,12 +674,7 @@ void UA_DiagnosticInfo_init(UA_DiagnosticInfo *p) {
 
 UA_StatusCode UA_DiagnosticInfo_copy(UA_DiagnosticInfo const *src, UA_DiagnosticInfo *dst) {
     UA_DiagnosticInfo_init(dst);
-    dst->hasSymbolicId          = src->hasSymbolicId;
-    dst->hasNamespaceUri        = src->hasNamespaceUri;
-    dst->hasLocalizedText       = src->hasLocalizedText;
-    dst->hasLocale              = src->hasLocale;
-    dst->hasAdditionalInfo      = src->hasAdditionalInfo;
-    dst->hasInnerStatusCode     = src->hasInnerStatusCode;
+    *((UA_Byte*)dst) = *((const UA_Byte*)src); // the bitfield
     
     dst->symbolicId = src->symbolicId;
     dst->namespaceUri = src->namespaceUri;