Kaynağa Gözat

use new load_from_file methods

jblanked 9 ay önce
ebeveyn
işleme
a13869e1ec
1 değiştirilmiş dosya ile 43 ekleme ve 14 silme
  1. 43 14
      flipper_http/flipper_http.c

+ 43 - 14
flipper_http/flipper_http.c

@@ -415,32 +415,43 @@ FuriString *flipper_http_load_from_file(char *file_path)
         return NULL;
     }
 
-    // Allocate a FuriString to hold the received data
-    FuriString *str_result = furi_string_alloc();
-    if (!str_result)
+    size_t file_size = storage_file_size(file);
+
+    // final memory check
+    if (memmgr_heap_get_max_free_block() < file_size)
     {
-        FURI_LOG_E(HTTP_TAG, "Failed to allocate FuriString");
+        FURI_LOG_E(HTTP_TAG, "Not enough heap to read file.");
         storage_file_close(file);
         storage_file_free(file);
         furi_record_close(RECORD_STORAGE);
         return NULL;
     }
 
-    // Reset the FuriString to ensure it's empty before reading
-    furi_string_reset(str_result);
-
-    // Define a buffer to hold the read data
-    uint8_t *buffer = (uint8_t *)malloc(MAX_FILE_SHOW);
+    // Allocate a buffer to hold the read data
+    uint8_t *buffer = (uint8_t *)malloc(file_size);
     if (!buffer)
     {
         FURI_LOG_E(HTTP_TAG, "Failed to allocate buffer");
-        furi_string_free(str_result);
         storage_file_close(file);
         storage_file_free(file);
         furi_record_close(RECORD_STORAGE);
         return NULL;
     }
 
+    // Allocate a FuriString to hold the received data
+    FuriString *str_result = furi_string_alloc();
+    if (!str_result)
+    {
+        FURI_LOG_E(HTTP_TAG, "Failed to allocate FuriString");
+        storage_file_close(file);
+        storage_file_free(file);
+        furi_record_close(RECORD_STORAGE);
+        return NULL;
+    }
+
+    // Reset the FuriString to ensure it's empty before reading
+    furi_string_reset(str_result);
+
     // Read data into the buffer
     size_t read_count = storage_file_read(file, buffer, MAX_FILE_SHOW);
     if (storage_file_get_error(file) != FSE_OK)
@@ -475,6 +486,12 @@ FuriString *flipper_http_load_from_file(char *file_path)
  */
 FuriString *flipper_http_load_from_file_with_limit(char *file_path, size_t limit)
 {
+    if (memmgr_heap_get_max_free_block() < limit)
+    {
+        FURI_LOG_E(HTTP_TAG, "Not enough heap to read file.");
+        return NULL;
+    }
+
     // Open the storage record
     Storage *storage = furi_record_open(RECORD_STORAGE);
     if (!storage)
@@ -501,7 +518,19 @@ FuriString *flipper_http_load_from_file_with_limit(char *file_path, size_t limit
         return NULL;
     }
 
-    if (memmgr_get_free_heap() < limit)
+    size_t file_size = storage_file_size(file);
+
+    if (file_size > limit)
+    {
+        FURI_LOG_E(HTTP_TAG, "File size exceeds limit: %d > %d", file_size, limit);
+        storage_file_close(file);
+        storage_file_free(file);
+        furi_record_close(RECORD_STORAGE);
+        return NULL;
+    }
+
+    // final memory check
+    if (memmgr_heap_get_max_free_block() < file_size)
     {
         FURI_LOG_E(HTTP_TAG, "Not enough heap to read file.");
         storage_file_close(file);
@@ -511,7 +540,7 @@ FuriString *flipper_http_load_from_file_with_limit(char *file_path, size_t limit
     }
 
     // Allocate a buffer to hold the read data
-    uint8_t *buffer = (uint8_t *)malloc(limit);
+    uint8_t *buffer = (uint8_t *)malloc(file_size);
     if (!buffer)
     {
         FURI_LOG_E(HTTP_TAG, "Failed to allocate buffer");
@@ -532,10 +561,10 @@ FuriString *flipper_http_load_from_file_with_limit(char *file_path, size_t limit
         furi_record_close(RECORD_STORAGE);
         return NULL;
     }
-    furi_string_reserve(str_result, limit);
+    furi_string_reserve(str_result, file_size);
 
     // Read data into the buffer
-    size_t read_count = storage_file_read(file, buffer, limit);
+    size_t read_count = storage_file_read(file, buffer, file_size);
     if (storage_file_get_error(file) != FSE_OK)
     {
         FURI_LOG_E(HTTP_TAG, "Error reading from file.");