Просмотр исходного кода

fix: plug memory/FILE leaks in image, flow and fileserver paths

- CImageBasis::Resize(int,int): free the old buffer like the destructor
  instead of leaking it; refuse on tmpImage. SetIndepended sets memsize.
- CAlignAndCutImage: free odata with free_psram_heap.
- ClassFlowControll::GetJPGStream: free the temporary image on all paths.
- ClassFlowAlignment, read_wlanini, server_file, ClassControllCamera
  (demo mode): fclose on early returns / check fopen results.
- ClassLogFile: non-KEEP branch uses a local FILE* (race on the shared one).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Victor Gladkikh 1 месяц назад
Родитель
Сommit
2e59a7fc9b

+ 2 - 0
code/components/jomjol_controlcamera/ClassControllCamera.cpp

@@ -1195,6 +1195,7 @@ void CCamera::useDemoMode(void)
     if (demoImage == NULL)
     {
         LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Unable to acquire required memory for demo image!");
+        fclose(fd);
         return;
     }
 
@@ -1243,6 +1244,7 @@ bool CCamera::loadNextDemoImage(camera_fb_t *fb)
         char buf[100];
         snprintf(buf, sizeof(buf), "Demo Image (%d bytes) is larger than provided buffer (%d bytes)!", (int)fileSize, DEMO_IMAGE_SIZE);
         LogFile.WriteToFile(ESP_LOG_ERROR, TAG, std::string(buf));
+        fclose(fp);
         return false;
     }
 

+ 4 - 0
code/components/jomjol_fileserver_ota/server_file.cpp

@@ -377,6 +377,7 @@ static esp_err_t send_datafile(httpd_req_t *req, bool send_full_file)
         if (fseek(fd, 0, SEEK_END))
         {
             LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Failed to get to end of file!");
+            fclose(fd);
             return ESP_FAIL;
         }
         else
@@ -387,6 +388,7 @@ static esp_err_t send_datafile(httpd_req_t *req, bool send_full_file)
             if (fseek(fd, pos - std::min((long)LOGFILE_LAST_PART_BYTES, pos), SEEK_SET))
             { // Go LOGFILE_LAST_PART_BYTES bytes back from EOF
                 LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Failed to go back " + to_string(std::min((long)LOGFILE_LAST_PART_BYTES, pos)) + " bytes within the file!");
+                fclose(fd);
                 return ESP_FAIL;
             }
         }
@@ -468,6 +470,7 @@ static esp_err_t send_logfile(httpd_req_t *req, bool send_full_file)
         if (fseek(fd, 0, SEEK_END))
         {
             LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Failed to get to end of file!");
+            fclose(fd);
             return ESP_FAIL;
         }
         else
@@ -478,6 +481,7 @@ static esp_err_t send_logfile(httpd_req_t *req, bool send_full_file)
             if (fseek(fd, pos - std::min((long)LOGFILE_LAST_PART_BYTES, pos), SEEK_SET))
             { // Go LOGFILE_LAST_PART_BYTES bytes back from EOF
                 LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Failed to go back " + to_string(std::min((long)LOGFILE_LAST_PART_BYTES, pos)) + " bytes within the file!");
+                fclose(fd);
                 return ESP_FAIL;
             }
         }

+ 5 - 0
code/components/jomjol_flowcontroll/ClassFlowAlignment.cpp

@@ -273,6 +273,11 @@ void ClassFlowAlignment::SaveReferenceAlignmentValues()
 
     pFile = fopen(FileStoreRefAlignment.c_str(), "w");
 
+    if (pFile == NULL) {
+        LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "SaveReferenceAlignmentValues: Failed to open " + FileStoreRefAlignment);
+        return;
+    }
+
     if (strlen(zwtime.c_str()) == 0) {
         time_t rawtime;
         struct tm *timeinfo;

+ 3 - 1
code/components/jomjol_flowcontroll/ClassFlowControll.cpp

@@ -843,7 +843,9 @@ esp_err_t ClassFlowControll::GetJPGStream(std::string _fn, httpd_req_t *req)
             }
             else {
                 LogFile.WriteToFile(ESP_LOG_WARN, TAG, "ClassFlowControll::GetJPGStream: Not enough memory to create alg_roi.jpg -> alg.jpg is going to be served!");
-                
+                delete _send;
+                _send = NULL;
+
                 if (flowalignment && flowalignment->ImageBasis->ImageOkay()) {
                     _send = flowalignment->ImageBasis;
                 }

+ 1 - 1
code/components/jomjol_image_proc/CAlignAndCutImage.cpp

@@ -129,7 +129,7 @@ void CAlignAndCutImage::CutAndSave(std::string _template1, int x1, int y1, int d
     
     RGBImageRelease();
 
-    stbi_image_free(odata);
+    free_psram_heap(std::string(TAG) + "->odata", odata);
 }
 
 void CAlignAndCutImage::CutAndSave(int x1, int y1, int dx, int dy, CImageBasis *_target)

+ 26 - 6
code/components/jomjol_image_proc/CImageBasis.cpp

@@ -705,20 +705,40 @@ void CImageBasis::SaveToFile(std::string _imageout)
 
 void CImageBasis::Resize(int _new_dx, int _new_dy)
 {
-    memsize = _new_dx * _new_dy * channels;
-    uint8_t* odata = (unsigned char*)malloc_psram_heap(std::string(TAG) + "->odata", memsize, MALLOC_CAP_SPIRAM);
+    // tmpImage lives in the shared PSRAM region and its destructor relies on that; never resize it in place
+    if ((rgb_image == NULL) || (name == "tmpImage")) {
+        LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Resize: not possible for image " + name);
+        return;
+    }
+
+    int new_memsize = _new_dx * _new_dy * channels;
+    uint8_t* odata = (unsigned char*)malloc_psram_heap(std::string(TAG) + "->CImageBasis Resize (" + name + ")", new_memsize, MALLOC_CAP_SPIRAM);
+
+    if (odata == NULL) {
+        LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Resize: Can't allocate enough memory: " + std::to_string(new_memsize));
+        return;
+    }
 
     RGBImageLock();
 
     stbir_resize_uint8(rgb_image, width, height, 0, odata, _new_dx, _new_dy, 0, channels);
 
-    rgb_image = (unsigned char*)malloc_psram_heap(std::string(TAG) + "->CImageBasis Resize (" + name + ")", memsize, MALLOC_CAP_SPIRAM);
-    memCopy(odata, rgb_image, memsize);
+    // Release the old buffer with the allocator it came from (same rule as the destructor)
+    if (!externalImage) {
+        if (memsize == 0) { // Loaded via STBI (STBI_MALLOC)
+            stbi_image_free(rgb_image);
+        }
+        else {
+            free_psram_heap(std::string(TAG) + "->CImageBasis Resize old (" + name + ")", rgb_image);
+        }
+    }
+
+    rgb_image = odata;
+    memsize = new_memsize;
+    externalImage = false; // The image now owns a normal PSRAM buffer
     width = _new_dx;
     height = _new_dy;
 
-    free_psram_heap(std::string(TAG) + "->odata", odata);
-
     RGBImageRelease();
 }
 

+ 1 - 1
code/components/jomjol_image_proc/CImageBasis.h

@@ -61,7 +61,7 @@ class CImageBasis
         bool ImageOkay();
         bool CopyFromMemory(uint8_t* _source, int _size);
 
-        void SetIndepended(){externalImage = false;};
+        void SetIndepended(){externalImage = false; memsize = width * height * channels;};
 
         void CreateEmptyImage(int _width, int _height, int _channels);
         void EmptyImage();

+ 2 - 0
code/components/jomjol_wlan/read_wlanini.cpp

@@ -233,6 +233,7 @@ bool ChangeHostName(std::string fn, std::string _newhostname)
     if (fgets(zw, sizeof(zw), pFile) == NULL) {
         line = "";
         LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "ChangeHostName: File opened, but empty or content not readable");
+        fclose(pFile);
         return false;
     }
     else {
@@ -317,6 +318,7 @@ bool ChangeRSSIThreshold(std::string fn, int _newrssithreshold)
     if (fgets(zw, sizeof(zw), pFile) == NULL) {
         line = "";
         LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "ChangeRSSIThreshold: File opened, but empty or content not readable");
+        fclose(pFile);
         return false;
     }
     else {