Skip to content
Snippets Groups Projects
Commit 1ba1302c authored by Jakob Olsson's avatar Jakob Olsson
Browse files

more error handling, some memory leak fixes

parent 242ece4b
No related branches found
No related tags found
No related merge requests found
......@@ -92,17 +92,15 @@ int set_pin(struct ubus_context *ctx, struct ubus_object *obj,
printf("invalid pin\n");
goto fail;
}
char *response = mobile_get_pin_status();
if (!response) {
printf("error getting pin_status\n");
goto fail;
}
struct json_object *parsed_response = json_tokener_parse(response);
struct json_object *rv_json;
json_object_object_get_ex(parsed_response, "pin_status", &rv_json);
if (!rv_json) {
printf("no pin_status available\n");
......@@ -112,13 +110,11 @@ int set_pin(struct ubus_context *ctx, struct ubus_object *obj,
free(response);
if (!json_object_get_int(rv_json)) {
json_object_put(parsed_response);
response = mobile_enable_pin(current_pin);
if (!response) {
printf("error enabling pin!\n");
goto fail;
}
parsed_response = json_tokener_parse(response);
json_object_object_get_ex(parsed_response, "result", &rv_json);
if (!rv_json) {
......@@ -127,6 +123,7 @@ int set_pin(struct ubus_context *ctx, struct ubus_object *obj,
}
if (strncmp(json_object_get_string(rv_json), "failure", strlen("failure")) == 0)
printf("Incorrect pin!\n");
free(response);
}
......@@ -136,7 +133,6 @@ int set_pin(struct ubus_context *ctx, struct ubus_object *obj,
printf("error setting pin!\n");
goto fail;
}
parsed_response = json_tokener_parse(response);
json_object_object_get_ex(parsed_response, "result", &rv_json);
if (!rv_json) {
......@@ -158,6 +154,7 @@ int disable_pin(struct ubus_context *ctx, struct ubus_object *obj,
{
struct blob_attr *tb[__PIN_MAX];
char pin[10] = {0};
int rv;
blobmsg_parse(pin_policy, __PIN_MAX, tb, blob_data(msg), blob_len(msg));
if (!tb[PIN]) {
......@@ -165,31 +162,50 @@ int disable_pin(struct ubus_context *ctx, struct ubus_object *obj,
goto fail;
}
strncpy(pin, (char *)blobmsg_data(tb[PIN]), 9);
validate_pin_format(pin);
rv = validate_pin_format(pin);
if (rv < 0) {
printf("invalid pin format!\n");
goto fail;
}
char *response = mobile_get_pin_status();
if (!response) {
printf("no response from get_pin_status!\n");
goto fail;
}
struct json_object *parsed_response = json_tokener_parse(response);
struct json_object *rv;
struct json_object *rv_json;
json_object_object_get_ex(parsed_response, "pin_status", &rv);
if (json_object_get_int(rv)) {
response = mobile_disable_pin(pin);
json_object_object_get_ex(parsed_response, "pin_status", &rv_json);
if (!rv_json) {
printf("no pin_status available in response!\n");
goto free_all;
}
if (json_object_get_int(rv_json)) {
free(response);
json_object_put(parsed_response);
response = mobile_disable_pin(pin);
if (!response) {
printf("eror disabling pin!\n");
goto fail;
}
parsed_response = json_tokener_parse(response);
json_object_object_get_ex(parsed_response, "result", &rv);
if (strncmp(json_object_get_string(rv), "failure", strlen("failure")) == 0) {
printf("Incorrect pin!\n");
goto print;
json_object_object_get_ex(parsed_response, "result", &rv_json);
if (!rv_json) {
printf("no result available in response!\n");
goto free_all;
}
if (strncmp(json_object_get_string(rv_json), "failure", strlen("failure")) == 0)
printf("Incorrect pin!\n");
} else {
printf("already disabled!\n");
}
print:
free_all:
write_to_ubus(parsed_response, ctx, req);
json_object_put(parsed_response);
fail:
free(response);
fail:
return 0;
}
......@@ -199,35 +215,55 @@ int enable_pin(struct ubus_context *ctx, struct ubus_object *obj,
{
struct blob_attr *tb[__PIN_MAX];
char pin[10] = {0};
int rv;
blobmsg_parse(pin_policy, __PIN_MAX, tb, blob_data(msg), blob_len(msg));
if (!tb[PIN]) {
printf("Please enter both a new pin and old pin!\n");
goto fail;
}
strncpy(pin, (char *)blobmsg_data(tb[PIN]), 9);
validate_pin_format(pin);
if (rv < 0) {
printf("invalid pin format!\n");
goto fail;
}
char *response = mobile_get_pin_status();
if (!response) {
printf("no response from get_pin_status!\n");
goto fail;
}
struct json_object *parsed_response = json_tokener_parse(response);
struct json_object *rv;
struct json_object *rv_json;
json_object_object_get_ex(parsed_response, "pin_status", &rv);
if (!json_object_get_int(rv)) {
response = mobile_enable_pin(pin);
json_object_object_get_ex(parsed_response, "pin_status", &rv_json);
if (!rv_json) {
printf("no pin_status available in response!\n");
goto free_all;
}
if (!json_object_get_int(rv_json)) {
free(response);
json_object_put(parsed_response);
response = mobile_enable_pin(pin);
if (!response) {
printf("no response from get_pin_status!\n");
goto fail;
}
parsed_response = json_tokener_parse(response);
json_object_object_get_ex(parsed_response, "result", &rv);
if (strncmp(json_object_get_string(rv), "failure", strlen("failure")) == 0) {
printf("Incorrect pin!\n");
goto print;
json_object_object_get_ex(parsed_response, "result", &rv_json);
if (!rv_json) {
printf("no result available in response!\n");
goto free_all;
}
if (strncmp(json_object_get_string(rv_json), "failure", strlen("failure")) == 0)
printf("Incorrect pin!\n");
} else {
printf("already enabled!\n");
}
print:
free_all:
write_to_ubus(parsed_response, ctx, req);
json_object_put(parsed_response);
fail:
......@@ -249,8 +285,16 @@ int verify_pin(struct ubus_context *ctx, struct ubus_object *obj,
}
strncpy(pin, (char *)blobmsg_data(tb[PIN]), 9);
validate_pin_format(pin);
if (rv < 0) {
printf("invalid pin format!\n");
goto fail;
}
char *response = mobile_set_pin(pin, pin);
if (!response) {
printf("no response from mobile_set_pin!\n");
goto fail;
}
struct json_object *parsed_response = json_tokener_parse(response);
write_to_ubus(parsed_response, ctx, req);
......@@ -265,9 +309,17 @@ int remaining_tries(struct ubus_context *ctx, struct ubus_object *obj,
struct blob_attr *msg)
{
char *response = mobile_get_pinnumber();
if (!response) {
printf("no response from mobile_get_pinnumber!\n");
goto fail;
}
struct json_object *parsed_response = json_tokener_parse(response);
write_to_ubus(parsed_response, ctx, req);
json_object_put(parsed_response);
free(response);
fail:
return 0;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment