Introduce read-only structure pointer typedefs for each of the state sub-structures, so the compiler will catch anywhere that a function would alter a structure it isn't expected to (#165).

This commit is contained in:
Andrew Wood
2025-07-26 20:51:28 +01:00
parent 3b0891b128
commit 870dfad9fd
2 changed files with 25 additions and 8 deletions
+17 -1
View File
@@ -440,6 +440,22 @@ typedef struct pvtransfercalc_s *pvtransfercalc_t;
typedef struct pvipcstate_s *pvipcstate_t;
typedef struct pvtransferstate_s *pvtransferstate_t;
/*
* Read-only counterparts to the above structure pointers, to be used in
* function declarations where the function definitely shouldn't be altering
* the contents of the structure.
*/
typedef const struct pvprogramstatus_s * const readonly_pvprogramstatus_t;
typedef const struct pvinputfiles_s * const readonly_pvinputfiles_t;
typedef const struct pvcontrol_s * const readonly_pvcontrol_t;
typedef const struct pvsignal_s * const readonly_pvsignal_t;
typedef const struct pvtransientflags_s * const readonly_pvtransientflags_t;
typedef const struct pvdisplay_s * const readonly_pvdisplay_t;
typedef const struct pvdisplay_segment_s * const readonly_pvdisplay_segment_t;
typedef const struct pvtransfercalc_s * const readonly_pvtransfercalc_t;
typedef const struct pvipcstate_s * const readonly_pvipcstate_t;
typedef const struct pvtransferstate_s * const readonly_pvtransferstate_t;
/*
* Structure containing the parameters used by formatters.
*/
@@ -490,7 +506,7 @@ typedef struct pvwatchfd_s *pvwatchfd_t;
void pv_error(pvstate_t, char *, ...);
int pv_main_loop(pvstate_t);
void pv_calculate_transfer_rate(pvtransfercalc_t, pvtransferstate_t, pvcontrol_t, pvdisplay_t, bool);
void pv_calculate_transfer_rate(pvtransfercalc_t, readonly_pvtransferstate_t, readonly_pvcontrol_t, readonly_pvdisplay_t, bool);
long pv_bound_long(long, long, long);
long pv_seconds_remaining(const off_t, const off_t, const long double);
+8 -7
View File
@@ -17,7 +17,7 @@
* rate, otherwise calulate the average rate from the difference between the
* current position + elapsed time pair, and the oldest pair in the buffer.
*/
static void pv__update_average_rate_history(pvtransfercalc_t calc, pvtransferstate_t transfer,
static void pv__update_average_rate_history(pvtransfercalc_t calc, readonly_pvtransferstate_t transfer,
unsigned int history_interval, long double rate)
{
size_t first = calc->history_first;
@@ -65,7 +65,7 @@ static void pv__update_average_rate_history(pvtransfercalc_t calc, pvtransfersta
/*
* Update all calculated transfer state (usually state->calc).
* Update all calculated transfer state in calc (usually from state->calc).
*
* If "final" is true, this is the final update, so calc->transfer_rate and
* calc->average_rate are given as an average over the whole transfer;
@@ -75,8 +75,8 @@ static void pv__update_average_rate_history(pvtransfercalc_t calc, pvtransfersta
* control->size is greater than zero, otherwise it will increase by 2 each
* call and wrap at 200.
*/
void pv_calculate_transfer_rate(pvtransfercalc_t calc, pvtransferstate_t transfer, pvcontrol_t control,
pvdisplay_t display, bool final)
void pv_calculate_transfer_rate(pvtransfercalc_t calc, readonly_pvtransferstate_t transfer,
readonly_pvcontrol_t control, readonly_pvdisplay_t display, bool final)
{
off_t bytes_since_last;
long double time_since_last, transfer_rate, average_rate;
@@ -141,12 +141,13 @@ void pv_calculate_transfer_rate(pvtransfercalc_t calc, pvtransferstate_t transfe
* period of the transfer.
*/
if (final) {
long double total_elapsed_seconds = (long double) (transfer->elapsed_seconds);
/* Safety check to avoid division by zero. */
if (transfer->elapsed_seconds < 0.000001)
transfer->elapsed_seconds = 0.000001;
if (total_elapsed_seconds < 0.000001)
total_elapsed_seconds = 0.000001;
average_rate =
(((long double) (transfer->transferred)) -
((long double) display->initial_offset)) / (long double) (transfer->elapsed_seconds);
((long double) display->initial_offset)) / total_elapsed_seconds;
transfer_rate = average_rate;
}