mirror of
https://github.com/Next-Flip/Momentum-Firmware.git
synced 2026-05-12 19:18:35 -07:00
[FL-3884] Proper integer parsing (#3839)
* feat: strint_to_uint32 and tests
* fix: permit explicit bases and prefixes
* feat: strint_to_{int32,uint16,int16}
* feat: strint_to_u?int64
* refactor: replace strtol, strtoul, sscanf with strint_to_*
* fix: api symbols
* docs: document parameter `end` of strint_to_uint_32
* style: apply changes requested by hedger
* refactor: fix pvs-studio diagnostic
* style: apply changes requested by CookiePLMonster
* fix: unused var
* fix: pointer type
* refactor: convert atoi to strint_to_*
* fix: strint_to_uint8 doesn't actually exist ._ .
* fix: memory leak
* style: address review comments
* Toolbox: couple small comments in the code and doxygen comment update. SubGhz, Loader: fix strint usage.
* Loader: fix incorrect cast
Co-authored-by: あく <alleteam@gmail.com>
This commit is contained in:
@@ -9,6 +9,7 @@
|
||||
#include <notification/notification_messages.h>
|
||||
#include <loader/loader.h>
|
||||
#include <lib/toolbox/args.h>
|
||||
#include <lib/toolbox/strint.h>
|
||||
|
||||
// Close to ISO, `date +'%Y-%m-%d %H:%M:%S %u'`
|
||||
#define CLI_DATE_FORMAT "%.4d-%.2d-%.2d %.2d:%.2d:%.2d %d"
|
||||
@@ -361,9 +362,9 @@ void cli_command_led(Cli* cli, FuriString* args, void* context) {
|
||||
}
|
||||
furi_string_free(light_name);
|
||||
// Read light value from the rest of the string
|
||||
char* end_ptr;
|
||||
uint32_t value = strtoul(furi_string_get_cstr(args), &end_ptr, 0);
|
||||
if(!(value < 256 && *end_ptr == '\0')) {
|
||||
uint32_t value;
|
||||
if(strint_to_uint32(furi_string_get_cstr(args), NULL, &value, 0) != StrintParseNoError ||
|
||||
value >= 256) {
|
||||
cli_print_usage("led", "<r|g|b|bl> <0-255>", furi_string_get_cstr(args));
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include <gui/elements.h>
|
||||
#include <furi.h>
|
||||
#include <assets_icons.h>
|
||||
#include <lib/toolbox/strint.h>
|
||||
|
||||
struct NumberInput {
|
||||
View* view;
|
||||
@@ -163,7 +164,11 @@ static void number_input_handle_right(NumberInputModel* model) {
|
||||
}
|
||||
|
||||
static bool is_number_too_large(NumberInputModel* model) {
|
||||
int64_t value = strtoll(furi_string_get_cstr(model->text_buffer), NULL, 10);
|
||||
int64_t value;
|
||||
if(strint_to_int64(furi_string_get_cstr(model->text_buffer), NULL, &value, 10) !=
|
||||
StrintParseNoError) {
|
||||
return true;
|
||||
}
|
||||
if(value > (int64_t)model->max_value) {
|
||||
return true;
|
||||
}
|
||||
@@ -171,7 +176,11 @@ static bool is_number_too_large(NumberInputModel* model) {
|
||||
}
|
||||
|
||||
static bool is_number_too_small(NumberInputModel* model) {
|
||||
int64_t value = strtoll(furi_string_get_cstr(model->text_buffer), NULL, 10);
|
||||
int64_t value;
|
||||
if(strint_to_int64(furi_string_get_cstr(model->text_buffer), NULL, &value, 10) !=
|
||||
StrintParseNoError) {
|
||||
return true;
|
||||
}
|
||||
if(value < (int64_t)model->min_value) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <cli/cli.h>
|
||||
#include <applications.h>
|
||||
#include <lib/toolbox/args.h>
|
||||
#include <lib/toolbox/strint.h>
|
||||
#include <notification/notification_messages.h>
|
||||
|
||||
static void loader_cli_print_usage(void) {
|
||||
@@ -89,18 +90,22 @@ static void loader_cli_close(Loader* loader) {
|
||||
|
||||
static void loader_cli_signal(FuriString* args, Loader* loader) {
|
||||
uint32_t signal;
|
||||
void* arg = NULL;
|
||||
uint32_t arg = 0;
|
||||
StrintParseError parse_err = 0;
|
||||
char* args_cstr = (char*)furi_string_get_cstr(args);
|
||||
parse_err |= strint_to_uint32(args_cstr, &args_cstr, &signal, 10);
|
||||
parse_err |= strint_to_uint32(args_cstr, &args_cstr, &arg, 16);
|
||||
|
||||
if(!sscanf(furi_string_get_cstr(args), "%lu %p", &signal, &arg)) {
|
||||
if(parse_err) {
|
||||
printf("Signal must be a decimal number\r\n");
|
||||
} else if(!loader_is_locked(loader)) {
|
||||
printf("No application is running\r\n");
|
||||
} else {
|
||||
const bool is_handled = loader_signal(loader, signal, arg);
|
||||
const bool is_handled = loader_signal(loader, signal, (void*)arg);
|
||||
printf(
|
||||
"Signal %lu with argument 0x%p was %s\r\n",
|
||||
signal,
|
||||
arg,
|
||||
(void*)arg,
|
||||
is_handled ? "handled" : "ignored");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,8 +3,9 @@
|
||||
|
||||
#include <cli/cli.h>
|
||||
#include <lib/toolbox/args.h>
|
||||
#include <lib/toolbox/md5_calc.h>
|
||||
#include <lib/toolbox/dir_walk.h>
|
||||
#include <lib/toolbox/md5_calc.h>
|
||||
#include <lib/toolbox/strint.h>
|
||||
#include <lib/toolbox/tar/tar_archive.h>
|
||||
#include <storage/storage.h>
|
||||
#include <storage/storage_sd_api.h>
|
||||
@@ -267,9 +268,8 @@ static void storage_cli_read_chunks(Cli* cli, FuriString* path, FuriString* args
|
||||
File* file = storage_file_alloc(api);
|
||||
|
||||
uint32_t buffer_size;
|
||||
int parsed_count = sscanf(furi_string_get_cstr(args), "%lu", &buffer_size);
|
||||
|
||||
if(parsed_count != 1) {
|
||||
if(strint_to_uint32(furi_string_get_cstr(args), NULL, &buffer_size, 10) !=
|
||||
StrintParseNoError) {
|
||||
storage_cli_print_usage();
|
||||
} else if(storage_file_open(file, furi_string_get_cstr(path), FSAM_READ, FSOM_OPEN_EXISTING)) {
|
||||
uint64_t file_size = storage_file_size(file);
|
||||
@@ -307,9 +307,8 @@ static void storage_cli_write_chunk(Cli* cli, FuriString* path, FuriString* args
|
||||
File* file = storage_file_alloc(api);
|
||||
|
||||
uint32_t buffer_size;
|
||||
int parsed_count = sscanf(furi_string_get_cstr(args), "%lu", &buffer_size);
|
||||
|
||||
if(parsed_count != 1) {
|
||||
if(strint_to_uint32(furi_string_get_cstr(args), NULL, &buffer_size, 10) !=
|
||||
StrintParseNoError) {
|
||||
storage_cli_print_usage();
|
||||
} else {
|
||||
if(storage_file_open(file, furi_string_get_cstr(path), FSAM_WRITE, FSOM_OPEN_APPEND)) {
|
||||
|
||||
Reference in New Issue
Block a user