Skip to content

Commit

Permalink
fix: core dump on non existant row name
Browse files Browse the repository at this point in the history
  • Loading branch information
rishithaminol committed Mar 23, 2018
1 parent 8f8db56 commit d519fcb
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
21 changes: 15 additions & 6 deletions httpd.c
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,8 @@ void print_uri_base(struct uri_base *ub)
/*! @} */ /* uri_tokenization */

/* @brief Send response to the user.
*
* On error make 't = NULL'.
*
* @param[in] header Used for reading path name (URL parameters).
* header has user reqested data.
Expand Down Expand Up @@ -279,7 +281,7 @@ static void send_json_response(int sockfd, struct myhttp_header *header, MYSQL *
tokens = tokenize_uri(header->url);
if (tokens == NULL) {
CM_ERROR("tokenization malfunction\n");
return;
goto error__;
}

te_entry = tokens->first;
Expand All @@ -292,7 +294,12 @@ static void send_json_response(int sockfd, struct myhttp_header *header, MYSQL *
sb = fetch_duration(db, "min_0", te_entry->value);
}
UNLOCK_SHIFT_COLUMN_LOCKER;
t = sb->first;

if (sb != NULL) {
t = sb->first;
} else {
goto error__;
}
} else if (strcmp(te_entry->key, "coinid") == 0) { /* /home/path/?coinid=bitcoin&range=1 or 2 */
char *coin_id = te_entry->value;
te_entry = te_entry->next;
Expand All @@ -317,17 +324,16 @@ static void send_json_response(int sockfd, struct myhttp_header *header, MYSQL *
}
} else {
CM_ERROR("invalid option for '%s' coinid\n", coin_id);
return;
goto error__;
}
} else {
CM_ERROR("tokenization malfunction\n");
return;
goto error__;
}
free_uri_base(tokens);

if (t == NULL) {
write(sockfd, "{\"error\": \"error occured\"}\n", strlen("{\"error\": \"error occured\"}\n"));
return;
goto error__;
}

int i = 1;
Expand All @@ -341,6 +347,9 @@ static void send_json_response(int sockfd, struct myhttp_header *header, MYSQL *
}

free_coin_status_base(sb);

error__:
write(sockfd, "{\"error\": \"error occured\"}\n", strlen("{\"error\": \"error occured\"}\n"));
} /* send_json_response */

struct __cb_args {
Expand Down
5 changes: 3 additions & 2 deletions mysql_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ struct coin_status_base *fetch_duration(MYSQL *db, const char *col1,
MYSQL_RES *result;
MYSQL_ROW row;

struct coin_status_base *coin_stat_base = init_coin_status_base();
struct coin_status_base *coin_stat_base;

LOCK_DB_ACCESS;

Expand All @@ -390,12 +390,13 @@ struct coin_status_base *fetch_duration(MYSQL *db, const char *col1,

if (mysql_num_fields(result) != 4) { /* this should return 3 columns */
CM_ERROR("%s\n", mysql_error(db));
free_coin_status_base(coin_stat_base);
mysql_free_result(result);
UNLOCK_DB_ACCESS;
return NULL;
}

coin_stat_base = init_coin_status_base();

while ((row = mysql_fetch_row(result))) {
struct coin_status *s;

Expand Down

0 comments on commit d519fcb

Please sign in to comment.