diff --git a/include/mastodont_application.h b/include/mastodont_application.h index 8432ba6..d0d48c4 100644 --- a/include/mastodont_application.h +++ b/include/mastodont_application.h @@ -68,6 +68,6 @@ int mastodont_obtain_oauth_token(mastodont_t* data, struct mstdnt_storage* storage, struct mstdnt_oauth_token* app); -void _mstdnt_val_application_call(cJSON* v, void* _type); +void _mstdnt_val_malloc_application_call(cJSON* v, void* _type); #endif /* MASTODONT_ACCOUNT */ diff --git a/include/mastodont_status.h b/include/mastodont_status.h index 96c034a..0b23354 100644 --- a/include/mastodont_status.h +++ b/include/mastodont_status.h @@ -43,7 +43,7 @@ struct mstdnt_status char* spoiler_text; struct mstdnt_attachment* media_attachments; size_t media_attachments_len; - struct mstdnt_app application; + struct mstdnt_app* application; struct mstdnt_status_pleroma pleroma; /* Rendering attributes */ diff --git a/src/application.c b/src/application.c index 7a337d9..a14c19a 100644 --- a/src/application.c +++ b/src/application.c @@ -22,6 +22,7 @@ static int mstdnt_app_json(cJSON* json, struct mstdnt_app* app) { + if (!json) return 1; /* Zero out */ memset(app, 0, sizeof(struct mstdnt_app)); @@ -35,7 +36,7 @@ static int mstdnt_app_json(cJSON* json, struct mstdnt_app* app) { "vapid_key", &(app->vapid_key), _mstdnt_val_string_call }, }; - for (cJSON* v = json->child; v; v = v->next) + for (cJSON* v = json; v; v = v->next) if (_mstdnt_key_val_ref(v, refs, _mstdnt_arr_len(refs))) return 1; @@ -58,8 +59,7 @@ static int mstdnt_token_json(cJSON* json, struct mstdnt_oauth_token* app) }; for (cJSON* v = json; v; v = v->next) - if (_mstdnt_key_val_ref(v->child, refs, _mstdnt_arr_len(refs)) == 1) - return 1; + _mstdnt_key_val_ref(v, refs, _mstdnt_arr_len(refs)); return 0; } @@ -69,10 +69,22 @@ static int mstdnt_token_json_callback(cJSON* json, void* args) return mstdnt_token_json(json, args); } -void _mstdnt_val_application_call(cJSON* v, void* _type) +void _mstdnt_val_malloc_application_call(cJSON* v, void* _type) { - struct mstdnt_app* type = _type; - mstdnt_app_json(type, v->child); + struct mstdnt_app** type = _type; + // We can skip an array size check by just seeing + // if the first value of the child is not zero + if (!(v->child && !cJSON_IsInvalid(v->child))) + { + *type = NULL; + return; + } + + + *type = calloc(1, sizeof(struct mstdnt_app)); + + if (*type) + mstdnt_app_json(v->child, *type); } int mastodont_register_app(mastodont_t* data, diff --git a/src/status.c b/src/status.c index c998d99..21a8ac9 100644 --- a/src/status.c +++ b/src/status.c @@ -115,7 +115,7 @@ int mstdnt_status_json(struct mstdnt_status* status, cJSON* js) { "bookmarked", &(status->bookmarked), _mstdnt_val_bool_call }, { "pinned", &(status->pinned), _mstdnt_val_bool_call }, { "reblogs_count", &(status->reblogs_count), _mstdnt_val_uint_call }, - { "application", &(status->application), _mstdnt_val_application_call }, + { "application", &(status->application), _mstdnt_val_malloc_application_call }, { "favourites_count", &(status->favourites_count), _mstdnt_val_uint_call }, { "replies_count", &(status->replies_count), _mstdnt_val_uint_call }, { "media_attachments", &att_args, _mstdnt_val_attachments_call }, @@ -571,6 +571,7 @@ void mstdnt_cleanup_status(struct mstdnt_status* status) mstdnt_cleanup_status(status->reblog); free(status->reblog); } + free(status->application); } void mstdnt_cleanup_statuses(struct mstdnt_status* statuses, size_t s)