Skip to content

Commit

Permalink
Move all time-unit logic into main(); protect against invalid values
Browse files Browse the repository at this point in the history
  • Loading branch information
halosghost committed Oct 20, 2016
1 parent b2f7b4c commit ad45d65
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 29 deletions.
28 changes: 28 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,34 @@ main (signed argc, char * argv []) {
}
}

if ( state.secs ) {
double count = 0;
char unit = 0;
sscanf(state.secs, "%lf%c", &count, &unit);

if ( !isfinite(count) && count > 0 ) {
pbpst_err(_("Sunset must be a finite, positive value"));
exit_status = EXIT_FAILURE; goto cleanup;
}

unsigned mult = 0;
switch ( unit ) {
case 'd': mult = 86400; break;
case 'h': mult = 3600; break;
case 'm': mult = 60; break;
default: mult = 1; break;
}

char * tmp = realloc(state.secs, 22);
if ( !tmp ) {
exit_status = EXIT_FAILURE; goto cleanup;
} else {
state.secs = tmp;
}

snprintf(state.secs, 22, "%u", (unsigned )(count * mult));
}

if ( !(db_loc = db_locate(&state)) || !(swp_db_loc = db_swp_init(db_loc)) ||
!(mem_db = db_read(db_loc)) ) {

Expand Down
1 change: 1 addition & 0 deletions src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <jansson.h>
#include <libintl.h>
#include <locale.h>
#include <math.h>

#define _(str) gettext(str)

Expand Down
17 changes: 1 addition & 16 deletions src/pb.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,24 +69,9 @@ pb_paste (const struct pbpst_state * s) {
if ( fc ) { status = CURLE_HTTP_POST_ERROR; goto cleanup; }

if ( s->secs ) {
double count = 0;
char unit = 0;
sscanf(s->secs, "%lf%c", &count, &unit);

unsigned mult = 0;
switch ( unit ) {
case 'd': mult = 86400; break;
case 'h': mult = 3600; break;
case 'm': mult = 60; break;
default: mult = 1; break;
}

char calc_secs [22];
snprintf(calc_secs, 22, "%u", (unsigned )count * mult);

fc = curl_formadd(&post, &last,
CURLFORM_COPYNAME, "s",
CURLFORM_COPYCONTENTS, calc_secs,
CURLFORM_COPYCONTENTS, s->secs,
CURLFORM_END);
if ( fc ) { status = CURLE_HTTP_POST_ERROR; goto cleanup; }
}
Expand Down
17 changes: 4 additions & 13 deletions src/pbpst_db.c
Original file line number Diff line number Diff line change
Expand Up @@ -384,21 +384,12 @@ db_add_entry (const struct pbpst_state * s, const char * userdata) {

if ( sunset_j && s->secs ) {
time_t curtime = time(NULL), offset = 0;

double count = 0;
char unit = 0;
sscanf(s->secs, "%lf%c", &count, &unit);

unsigned mult = 0;
switch ( unit ) {
case 'd': mult = 86400; break;
case 'h': mult = 3600; break;
case 'm': mult = 60; break;
default: mult = 1; break;
if ( sscanf(s->secs, "%ld", &offset) == EOF ) {
signed errsv = errno;
print_err2(_("Failed to scan offset"), strerror(errsv));
status = EXIT_FAILURE; goto cleanup;
}

offset = (time_t )((unsigned )count * mult);

if ( !(sunset = malloc(12)) ) {
print_err2(_("Failed to store sunset epoch"), _("Out of Memory"));
status = EXIT_FAILURE; goto cleanup;
Expand Down

0 comments on commit ad45d65

Please sign in to comment.