(formats/json/rjson.c) Get rid of some implicit strlens and pass

size parameter to the functions instead˜
This commit is contained in:
libretroadmin 2022-07-17 06:58:22 +02:00
parent 0a12062e1c
commit e4b33fd0e6
6 changed files with 21 additions and 23 deletions

View File

@ -24,7 +24,7 @@
#include <stdio.h> /* snprintf, vsnprintf */ #include <stdio.h> /* snprintf, vsnprintf */
#include <stdarg.h> /* va_list */ #include <stdarg.h> /* va_list */
#include <string.h> /* memcpy, strlen */ #include <string.h> /* memcpy */
#include <stdint.h> /* int64_t */ #include <stdint.h> /* int64_t */
#include <stdlib.h> /* malloc, realloc, atof, atoi */ #include <stdlib.h> /* malloc, realloc, atof, atoi */
@ -871,9 +871,9 @@ rjson_t *rjson_open_buffer(const void *buffer, size_t size)
return json; return json;
} }
rjson_t *rjson_open_string(const char *string) rjson_t *rjson_open_string(const char *string, size_t len)
{ {
return rjson_open_buffer(string, strlen(string)); return rjson_open_buffer(string, len);
} }
static int _rjson_stream_io(void* buf, int len, void *user) static int _rjson_stream_io(void* buf, int len, void *user)
@ -1123,7 +1123,7 @@ enum rjson_type rjson_parse(rjson_t *json, void* context,
} }
} }
bool rjson_parse_quick(const char *string, void* context, char option_flags, bool rjson_parse_quick(const char *string, size_t len, void* context, char option_flags,
bool (*object_member_handler)(void *context, const char *str, size_t len), bool (*object_member_handler)(void *context, const char *str, size_t len),
bool (*string_handler )(void *context, const char *str, size_t len), bool (*string_handler )(void *context, const char *str, size_t len),
bool (*number_handler )(void *context, const char *str, size_t len), bool (*number_handler )(void *context, const char *str, size_t len),
@ -1138,7 +1138,7 @@ bool rjson_parse_quick(const char *string, void* context, char option_flags,
const char *user_data[2]; const char *user_data[2];
rjson_t json; rjson_t json;
user_data[0] = string; user_data[0] = string;
user_data[1] = string + strlen(string); user_data[1] = string + len;
_rjson_setup(&json, _rjson_buffer_io, (void*)user_data, sizeof(json.input_buf)); _rjson_setup(&json, _rjson_buffer_io, (void*)user_data, sizeof(json.input_buf));
rjson_set_options(&json, option_flags); rjson_set_options(&json, option_flags);
if (rjson_parse(&json, context, if (rjson_parse(&json, context,

View File

@ -69,7 +69,7 @@ struct RFILE;
rjson_t *rjson_open_stream(struct intfstream_internal *stream); rjson_t *rjson_open_stream(struct intfstream_internal *stream);
rjson_t *rjson_open_rfile(struct RFILE *rfile); rjson_t *rjson_open_rfile(struct RFILE *rfile);
rjson_t *rjson_open_buffer(const void *buffer, size_t size); rjson_t *rjson_open_buffer(const void *buffer, size_t size);
rjson_t *rjson_open_string(const char *string); rjson_t *rjson_open_string(const char *string, size_t len);
rjson_t *rjson_open_user(rjson_io_t io, void *user_data, int io_block_size); rjson_t *rjson_open_user(rjson_io_t io, void *user_data, int io_block_size);
/* Free the parser instance created with rjson_open_* */ /* Free the parser instance created with rjson_open_* */
@ -157,7 +157,7 @@ enum rjson_type rjson_parse(rjson_t *json, void* context,
/* A simpler interface to parse a JSON in memory. This will avoid any memory /* A simpler interface to parse a JSON in memory. This will avoid any memory
* allocations unless the document contains strings longer than 512 characters. * allocations unless the document contains strings longer than 512 characters.
* In the error handler, error will be "" if any of the other handlers aborted. */ * In the error handler, error will be "" if any of the other handlers aborted. */
bool rjson_parse_quick(const char *string, void* context, char option_flags, bool rjson_parse_quick(const char *string, size_t len, void* context, char option_flags,
bool (*object_member_handler)(void *context, const char *str, size_t len), bool (*object_member_handler)(void *context, const char *str, size_t len),
bool (*string_handler )(void *context, const char *str, size_t len), bool (*string_handler )(void *context, const char *str, size_t len),
bool (*number_handler )(void *context, const char *str, size_t len), bool (*number_handler )(void *context, const char *str, size_t len),

View File

@ -6048,10 +6048,9 @@ static void netplay_refresh_rooms_cb(retro_task_t *task, void *task_data,
{ {
int room_count; int room_count;
netplay_rooms_parse(room_data); netplay_rooms_parse(room_data, strlen(room_data));
room_count = netplay_rooms_get_count(); if ((room_count = netplay_rooms_get_count()) > 0)
if (room_count > 0)
{ {
net_st->room_list = (struct netplay_room*)calloc(room_count, net_st->room_list = (struct netplay_room*)calloc(room_count,
sizeof(*net_st->room_list)); sizeof(*net_st->room_list));

View File

@ -169,17 +169,15 @@ static void handle_discord_join_cb(retro_task_t *task, void *task_data,
if (data->status != 200) if (data->status != 200)
goto done; goto done;
room_data = (char*)malloc(data->len + 1); if (!(room_data = (char*)malloc(data->len + 1)))
if (!room_data)
goto done; goto done;
memcpy(room_data, data->data, data->len); memcpy(room_data, data->data, data->len);
room_data[data->len] = '\0'; room_data[data->len] = '\0';
netplay_rooms_parse(room_data); netplay_rooms_parse(room_data, strlen(room_data));
free(room_data); free(room_data);
room = netplay_room_get(0); if ((room = netplay_room_get(0)))
if (room)
{ {
if (room->host_method == NETPLAY_HOST_METHOD_MITM) if (room->host_method == NETPLAY_HOST_METHOD_MITM)
snprintf(hostname, sizeof(hostname), "%s|%d|%s", snprintf(hostname, sizeof(hostname), "%s|%d|%s",
@ -204,13 +202,14 @@ done:
static void handle_discord_join(const char *secret) static void handle_discord_join(const char *secret)
{ {
int room_id;
char url[512]; char url[512];
discord_state_t *discord_st = &discord_state_st;
int room_id = (int)strtol(secret, NULL, 10);
if (room_id) if ((room_id = (int)strtol(secret, NULL, 10)))
{ {
snprintf(discord_st->peer_party_id, sizeof(discord_st->peer_party_id), discord_state_t *discord_st = &discord_state_st;
snprintf(discord_st->peer_party_id,
sizeof(discord_st->peer_party_id),
"%d", room_id); "%d", room_id);
strlcpy(url, FILE_PATH_LOBBY_LIBRETRO_URL, sizeof(url)); strlcpy(url, FILE_PATH_LOBBY_LIBRETRO_URL, sizeof(url));

View File

@ -265,7 +265,7 @@ bool netplay_decode_hostname(const char *hostname,
char *address, unsigned *port, char *session, size_t len); char *address, unsigned *port, char *session, size_t len);
bool netplay_is_lan_address(struct sockaddr_in *addr); bool netplay_is_lan_address(struct sockaddr_in *addr);
int netplay_rooms_parse(const char *buf); int netplay_rooms_parse(const char *buf, size_t len);
int netplay_rooms_get_count(void); int netplay_rooms_get_count(void);
struct netplay_room *netplay_room_get(int index); struct netplay_room *netplay_room_get(int index);
void netplay_rooms_free(void); void netplay_rooms_free(void);

View File

@ -285,7 +285,7 @@ void netplay_rooms_free(void)
net_st->rooms_data = NULL; net_st->rooms_data = NULL;
} }
int netplay_rooms_parse(const char *buf) int netplay_rooms_parse(const char *buf, size_t len)
{ {
struct netplay_json_context ctx; struct netplay_json_context ctx;
net_driver_state_t *net_st = networking_state_get_ptr(); net_driver_state_t *net_st = networking_state_get_ptr();
@ -300,7 +300,7 @@ int netplay_rooms_parse(const char *buf)
net_st->rooms_data = (struct netplay_rooms*) net_st->rooms_data = (struct netplay_rooms*)
calloc(1, sizeof(*net_st->rooms_data)); calloc(1, sizeof(*net_st->rooms_data));
rjson_parse_quick(buf, &ctx, 0, rjson_parse_quick(buf, len, &ctx, 0,
netplay_json_object_member, netplay_json_object_member,
netplay_json_string, netplay_json_string,
netplay_json_number, netplay_json_number,