Skip to content

Commit

Permalink
itsa: Simplify next_fib()
Browse files Browse the repository at this point in the history
Massively simply the next_fib() function for doing back-off retries.

In the process it fixes the following compiler warning from GCC 13.1.1

In function ‘next_fib’,
    inlined from ‘get_calculation’ at itsa.c:554:15:
itsa.c:177:20: warning: ‘state’ may be used uninitialized [-Wmaybe-uninitialize]
  177 |                 Fn = *state + last;
      |                 ~~~^~~~~~~~~~~~~~~
itsa.c: In function ‘get_calculation’:
itsa.c:543:13: note: ‘state’ was declared here
  543 |         int state;
      |             ^~~~~

We're not using threads, so there's no need for the state to be
maintained externally, just use a static int in next_fib() for it.

Signed-off-by: Andrew Clayton <[email protected]>
  • Loading branch information
ac000 committed Jul 25, 2023
1 parent 578e960 commit 1e10d2a
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions src/itsa.c
Original file line number Diff line number Diff line change
Expand Up @@ -165,20 +165,18 @@ static json_t *get_result_json(const char *buf)
* For doing request back-off, following the Fibonaci Sequence
* (skipping 0)
*/
static int next_fib(int last, int *state)
static int next_fib(int last)
{
int Fn;
static int state;

if (last == -1)
last = *state = 0;
else if (last == 0)
if (last == -1) {
state = 0;
return 1;
else
Fn = *state + last;
}

*state = last;
if (*state == 0)
return 1;
Fn = state + last;
state = last;

return Fn;
}
Expand Down Expand Up @@ -540,7 +538,6 @@ static int get_calculation(const char *tax_year, const char *cid)
char *jbuf;
int ret = -1;
int err;
int state;
int fib_sleep = -1;

again:
Expand All @@ -551,7 +548,7 @@ static int get_calculation(const char *tax_year, const char *cid)
mtd_err2str(err), jbuf);
goto out_free;
} else if (err == -MTD_ERR_REQUEST) {
fib_sleep = next_fib(fib_sleep, &state);
fib_sleep = next_fib(fib_sleep);
printic("Trying to get calculation again in "
"#BOLD#%d#RST# second(s)\n", fib_sleep);
fflush(stdout);
Expand Down

0 comments on commit 1e10d2a

Please sign in to comment.