Selaa lähdekoodia

Fix out of memory check

Stefan Profanter 7 vuotta sitten
vanhempi
commit
e2b13daa8b
1 muutettua tiedostoa jossa 19 lisäystä ja 10 poistoa
  1. 19 10
      src/server/ua_services_discovery.c

+ 19 - 10
src/server/ua_services_discovery.c

@@ -386,8 +386,13 @@ process_RegisterServer(UA_Server *server, UA_Session *session,
 
     if(requestServer->semaphoreFilePath.length) {
 #ifdef UA_ENABLE_DISCOVERY_SEMAPHORE
-        // todo: malloc may fail: return a statuscode
         char* filePath = (char *)UA_malloc(sizeof(char)*requestServer->semaphoreFilePath.length+1);
+        if (!filePath) {
+            UA_LOG_ERROR_SESSION(server->config.logger, session,
+                                   "Cannot allocate memory for semaphore path. Out of memory.", NULL);
+            responseHeader->serviceResult = UA_STATUSCODE_BADOUTOFMEMORY;
+            return;
+        }
         memcpy(filePath, requestServer->semaphoreFilePath.data, requestServer->semaphoreFilePath.length );
         filePath[requestServer->semaphoreFilePath.length] = '\0';
         if(access( filePath, 0 ) == -1) {
@@ -517,18 +522,22 @@ void UA_Discovery_cleanupTimedOut(UA_Server *server, UA_DateTime nowMonotonic) {
             size_t fpSize = sizeof(char)*current->registeredServer.semaphoreFilePath.length+1;
             // todo: malloc may fail: return a statuscode
             char* filePath = (char *)UA_malloc(fpSize);
-            memcpy(filePath, current->registeredServer.semaphoreFilePath.data,
-                   current->registeredServer.semaphoreFilePath.length );
-            filePath[current->registeredServer.semaphoreFilePath.length] = '\0';
+            if (filePath) {
+                memcpy(filePath, current->registeredServer.semaphoreFilePath.data,
+                       current->registeredServer.semaphoreFilePath.length );
+                filePath[current->registeredServer.semaphoreFilePath.length] = '\0';
 #ifdef UNDER_CE
-           FILE *fp = fopen(filePath,"rb");
-           semaphoreDeleted = (fp==NULL);
-           if(fp)
-             fclose(fp);
+                FILE *fp = fopen(filePath,"rb");
+                semaphoreDeleted = (fp==NULL);
+                if(fp)
+                    fclose(fp);
 #else
-           semaphoreDeleted = access( filePath, 0 ) == -1;
+                semaphoreDeleted = access( filePath, 0 ) == -1;
 #endif
-           UA_free(filePath);
+                UA_free(filePath);
+            } else {
+                UA_LOG_ERROR(server->config.logger, UA_LOGCATEGORY_SERVER, "Cannot check registration semaphore. Out of memory");
+            }
         }
 #endif