Skip to content

Commit

Permalink
improve error handling for achievement unlocks (libretro#11916)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamiras authored Jan 21, 2021
1 parent cff0a4e commit 015576b
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 11 deletions.
36 changes: 33 additions & 3 deletions cheevos/cheevos.c
Original file line number Diff line number Diff line change
Expand Up @@ -621,12 +621,34 @@ static void rcheevos_async_task_callback(
retro_task_t* task, void* task_data, void* user_data, const char* error)
{
rcheevos_async_io_request* request = (rcheevos_async_io_request*)user_data;
http_transfer_data_t* data = (http_transfer_data_t*)task_data;

if (!error)
{
char buffer[224];
const http_transfer_data_t* data = (http_transfer_data_t*)task->task_data;
if (rcheevos_get_json_error(data->data, buffer, sizeof(buffer)) == RC_OK)
char buffer[224] = "";
if (!data)
{
/* server did not return HTTP headers */
snprintf(buffer, sizeof(buffer), "Server communication error");
}
else if (data->status != 200)
{
/* server returned an error via status code - check to see if it also returned a JSON error */
if (!data->data || rcheevos_get_json_error(data->data, buffer, sizeof(buffer)) != RC_OK)
snprintf(buffer, sizeof(buffer), "HTTP error code: %d", data->status);
}
else if (!data->data || !data->len)
{
/* server sent an empty response without an error status code */
snprintf(buffer, sizeof(buffer), "No response from server");
}
else
{
/* server sent a message - assume its JSON and check for a JSON error */
rcheevos_get_json_error(data->data, buffer, sizeof(buffer));
}

if (buffer[0])
{
char errbuf[256];
snprintf(errbuf, sizeof(errbuf), "%s %u: %s", request->failure_message, request->id, buffer);
Expand Down Expand Up @@ -669,6 +691,14 @@ static void rcheevos_async_task_callback(

CHEEVOS_ERR(RCHEEVOS_TAG "%s %u: %s\n", request->failure_message, request->id, error);
}

if (data)
{
if (data->data)
free(data->data);

free(data);
}
}

static void rcheevos_activate_achievements(rcheevos_locals_t *locals,
Expand Down
2 changes: 1 addition & 1 deletion menu/cbs/menu_cbs_ok.c
Original file line number Diff line number Diff line change
Expand Up @@ -4248,7 +4248,7 @@ static void cb_net_generic(retro_task_t *task,
menu->core_buf = NULL;
menu->core_len = 0;

if (!data || err)
if (!data || err || !data->data)
goto finish;

menu->core_buf = (char*)malloc((data->len+1) * sizeof(char));
Expand Down
4 changes: 2 additions & 2 deletions retroarch.c
Original file line number Diff line number Diff line change
Expand Up @@ -5947,7 +5947,7 @@ static void handle_discord_join_cb(retro_task_t *task,
struct rarch_state *p_rarch = &rarch_st;
discord_state_t *discord_st = &p_rarch->discord_st;

if (!data || err)
if (!data || err || !data->data)
goto finish;

data->data = (char*)realloc(data->data, data->len + 1);
Expand Down Expand Up @@ -10648,7 +10648,7 @@ static void handle_translation_cb(
RARCH_LOG("RESULT FROM AI SERVICE...\n");
#endif

if (!data || error)
if (!data || error || !data->data)
goto finish;

json = rjson_open_buffer(data->data, data->len);
Expand Down
23 changes: 18 additions & 5 deletions tasks/task_http.c
Original file line number Diff line number Diff line change
Expand Up @@ -191,15 +191,28 @@ static void task_http_transfer_handler(retro_task_t *task)
free(tmp);

if (task_get_cancelled(task))
{
task_set_error(task, strdup("Task cancelled."));
else if (!task->mute)
task_set_error(task, strdup("Download failed."));
}
else
{
data = (http_transfer_data_t*)malloc(sizeof(*data));
data->data = NULL;
data->len = 0;
data->status = net_http_status(http->handle);

task_set_data(task, data);

if (!task->mute)
task_set_error(task, strdup("Download failed."));
}
}
else
{
data = (http_transfer_data_t*)malloc(sizeof(*data));
data->data = tmp;
data->len = len;
data = (http_transfer_data_t*)malloc(sizeof(*data));
data->data = tmp;
data->len = len;
data->status = net_http_status(http->handle);

task_set_data(task, data);
}
Expand Down
1 change: 1 addition & 0 deletions tasks/tasks_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ typedef struct
{
char *data;
size_t len;
int status;
} http_transfer_data_t;

void *task_push_http_transfer(const char *url, bool mute, const char *type,
Expand Down

0 comments on commit 015576b

Please sign in to comment.