ソースを参照

Refactor to fix memleak, unsafe strtok and assets path

Willy-JL 1 年間 前
コミット
7692f85773

+ 1 - 1
ble_spam/application.fam

@@ -6,7 +6,7 @@ App(
     stack_size=2 * 1024,
     fap_icon="ble_spam_10px.png",
     fap_category="Bluetooth",
-    fap_file_assets="spamlists",
+    fap_file_assets="assets",
     fap_author="@Willy-JL @ECTO-1A @Spooks4576",
     fap_weburl="https://github.com/Next-Flip/Momentum-Apps/tree/dev/ble_spam",
     fap_version="6.0",

+ 4 - 0
ble_spam/assets/nameflood.txt

@@ -0,0 +1,4 @@
+🛞MNTM-FW🛞
+YAPYAPYAP
+Flipper 🐬
+👋

+ 4 - 0
ble_spam/assets/swiftpair.txt

@@ -0,0 +1,4 @@
+🛞MNTM-FW🛞
+YAPYAPYAP
+Flipper 🐬
+👋

+ 32 - 34
ble_spam/protocols/nameflood.c

@@ -1,54 +1,52 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
 #include "nameflood.h"
 #include "_protocols.h"
+
 #include <storage/storage.h>
+#include <toolbox/stream/file_stream.h>
 
 // Hacked together by @Willy-JL
 
+static char names[256][sizeof(((NamefloodCfg*)0)->name)];
+static uint8_t names_count = 0;
+
 static const char* get_name(const Payload* payload) {
     UNUSED(payload);
     return "NameFlood";
 }
 
-static const char* make_name(const Payload* payload) {
-    UNUSED(payload);
-    static const char* names[256];
-    static uint8_t names_count = 0;
-
+static const char* random_name() {
     if(names_count == 0) {
+        // Fill random names
         Storage* storage = furi_record_open(RECORD_STORAGE);
-        if(storage) {
-            File* file = storage_file_alloc(storage);
-            if(storage_file_open(
-                   file,
-                   "/ext/apps_assets/ble_spam/floodlist.txt",
-                   FSAM_READ,
-                   FSOM_OPEN_EXISTING)) {
-                char line[256];
-                uint64_t bytes_read = storage_file_read(file, line, sizeof(line));
-                if(bytes_read > 0) {
-                    line[bytes_read] = '\0';
-
-                    char* name = strtok(line, ",");
-                    while(name && names_count < 255) {
-                        names[names_count++] = strdup(name);
-                        name = strtok(NULL, ",");
-                    }
+        Stream* stream = file_stream_alloc(storage);
+        FuriString* line = furi_string_alloc();
+        if(!storage_file_exists(storage, APP_DATA_PATH("nameflood.txt"))) {
+            // Copy default names
+            storage_common_copy(
+                storage, APP_ASSETS_PATH("nameflood.txt"), APP_DATA_PATH("nameflood.txt"));
+        }
+        if(file_stream_open(
+               stream, APP_DATA_PATH("nameflood.txt"), FSAM_READ, FSOM_OPEN_EXISTING)) {
+            while(stream_read_line(stream, line)) {
+                furi_string_replace_all(line, "\r", "");
+                furi_string_replace_all(line, "\n", "");
+                if(furi_string_size(line)) {
+                    strlcpy(names[names_count++], furi_string_get_cstr(line), sizeof(names[0]));
                 }
-                storage_file_close(file);
             }
-            storage_file_free(file);
-            furi_record_close(RECORD_STORAGE);
+        }
+        furi_string_free(line);
+        file_stream_close(stream);
+        stream_free(stream);
+        furi_record_close(RECORD_STORAGE);
+
+        if(names_count == 0) {
+            // Add fallback if list is empty
+            strlcpy(names[names_count++], "NameFlood", sizeof(names[0]));
         }
     }
 
-    if(names_count == 0) {
-        return "NameFlood";
-    } else {
-        return names[rand() % names_count];
-    }
+    return names[rand() % names_count];
 }
 
 static void make_packet(uint8_t* _size, uint8_t** _packet, Payload* payload) {
@@ -58,7 +56,7 @@ static void make_packet(uint8_t* _size, uint8_t** _packet, Payload* payload) {
     switch(cfg ? payload->mode : PayloadModeRandom) {
     case PayloadModeRandom:
     default:
-        name = make_name(payload);
+        name = random_name();
         break;
     case PayloadModeValue:
         name = cfg->name;

+ 35 - 34
ble_spam/protocols/swiftpair.c

@@ -1,52 +1,53 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
 #include "swiftpair.h"
 #include "_protocols.h"
+
 #include <storage/storage.h>
+#include <toolbox/stream/file_stream.h>
 
 // Hacked together by @Willy-JL and @Spooks4576
 // Documentation at https://learn.microsoft.com/en-us/windows-hardware/design/component-guidelines/bluetooth-swift-pair
 
-static const char* make_name(const Payload* payload) {
+static char names[256][sizeof(((SwiftpairCfg*)0)->name)];
+static uint8_t names_count = 0;
+
+static const char* get_name(const Payload* payload) {
     UNUSED(payload);
-    static const char* names[256];
-    static uint8_t names_count = 0;
+    return "SwiftPair";
+}
 
+static const char* random_name() {
     if(names_count == 0) {
+        // Fill random names
         Storage* storage = furi_record_open(RECORD_STORAGE);
-        if(storage) {
-            File* file = storage_file_alloc(storage);
-            if(storage_file_open(
-                   file, "/ext/apps_assets/ble_spam/winlist.txt", FSAM_READ, FSOM_OPEN_EXISTING)) {
-                char line[256];
-                uint64_t bytes_read = storage_file_read(file, line, sizeof(line));
-                if(bytes_read > 0) {
-                    line[bytes_read] = '\0';
-
-                    char* name = strtok(line, ",");
-                    while(name && names_count < 255) {
-                        names[names_count++] = strdup(name);
-                        name = strtok(NULL, ",");
-                    }
+        Stream* stream = file_stream_alloc(storage);
+        FuriString* line = furi_string_alloc();
+        if(!storage_file_exists(storage, APP_DATA_PATH("swiftpair.txt"))) {
+            // Copy default names
+            storage_common_copy(
+                storage, APP_ASSETS_PATH("swiftpair.txt"), APP_DATA_PATH("swiftpair.txt"));
+        }
+        if(file_stream_open(
+               stream, APP_DATA_PATH("swiftpair.txt"), FSAM_READ, FSOM_OPEN_EXISTING)) {
+            while(stream_read_line(stream, line)) {
+                furi_string_replace_all(line, "\r", "");
+                furi_string_replace_all(line, "\n", "");
+                if(furi_string_size(line)) {
+                    strlcpy(names[names_count++], furi_string_get_cstr(line), sizeof(names[0]));
                 }
-                storage_file_close(file);
             }
-            storage_file_free(file);
-            furi_record_close(RECORD_STORAGE);
+        }
+        furi_string_free(line);
+        file_stream_close(stream);
+        stream_free(stream);
+        furi_record_close(RECORD_STORAGE);
+
+        if(names_count == 0) {
+            // Add fallback if list is empty
+            strlcpy(names[names_count++], "SwiftPair", sizeof(names[0]));
         }
     }
 
-    if(names_count == 0) {
-        return "NameFlood";
-    } else {
-        return names[rand() % names_count];
-    }
-}
-
-static const char* get_name(const Payload* payload) {
-    UNUSED(payload);
-    return "SwiftPair";
+    return names[rand() % names_count];
 }
 
 static void make_packet(uint8_t* _size, uint8_t** _packet, Payload* payload) {
@@ -56,7 +57,7 @@ static void make_packet(uint8_t* _size, uint8_t** _packet, Payload* payload) {
     switch(cfg ? payload->mode : PayloadModeRandom) {
     case PayloadModeRandom:
     default:
-        name = make_name(payload);
+        name = random_name();
         break;
     case PayloadModeValue:
         name = cfg->name;

+ 0 - 1
ble_spam/spamlists/floodlist.txt

@@ -1 +0,0 @@
-🛞MNTM-FW🛞,YAPYAPYAP,Flipper 🐬,👋

+ 0 - 1
ble_spam/spamlists/winlist.txt

@@ -1 +0,0 @@
-🛞MNTM-FW🛞,YAPYAPYAP,Flipper 🐬,👋