From 98544a496f58864028517747584d49bd90355e33 Mon Sep 17 00:00:00 2001 From: Matthew Fernandez Date: Wed, 2 Nov 2022 22:02:42 -0700 Subject: [PATCH] cdt: use 'size_t' types for counting stats instead of 'int' MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit This squashes two compiler warnings: dtstat.c: In function ‘dtstat’: dtstat.c:60:59: warning: conversion to ‘long unsigned int’ from ‘int’ may change the sign of the result [-Wsign-conversion] 60 | if(!(Count = malloc((ds->dt_max+1)*sizeof(int))) ) | ^ dtstat.c:74:65: warning: conversion to ‘long unsigned int’ from ‘int’ may change the sign of the result [-Wsign-conversion] 74 | if(!(Count = malloc((ds->dt_n+1)*sizeof(int))) ) | ^ It also allows these functions to now count beyond 2³¹ - 1. --- lib/cdt/cdt.h | 6 +++--- lib/cdt/dtstat.c | 20 +++++++++----------- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/lib/cdt/cdt.h b/lib/cdt/cdt.h index 6e3aa44c8..a285ac26b 100644 --- a/lib/cdt/cdt.h +++ b/lib/cdt/cdt.h @@ -119,9 +119,9 @@ struct _dt_s struct _dtstat_s { int dt_meth; /* method type */ int dt_size; /* number of elements */ - int dt_n; /* number of chains or levels */ - int dt_max; /* max size of a chain or a level */ - int* dt_count; /* counts of chains or levels by size */ + size_t dt_n; // number of chains or levels + size_t dt_max; // max size of a chain or a level + size_t* dt_count; // counts of chains or levels by size }; /* flag set if the last search operation actually found the object */ diff --git a/lib/cdt/dtstat.c b/lib/cdt/dtstat.c index 5c40fb1dc..91e713828 100644 --- a/lib/cdt/dtstat.c +++ b/lib/cdt/dtstat.c @@ -6,8 +6,7 @@ ** Written by Kiem-Phong Vo (5/25/96) */ -static void dttstat(Dtstat_t* ds, Dtlink_t* root, int depth, int* level) -{ +static void dttstat(Dtstat_t *ds, Dtlink_t *root, size_t depth, size_t *level) { if(root->left) dttstat(ds,root->left,depth+1,level); if(root->right) @@ -18,13 +17,12 @@ static void dttstat(Dtstat_t* ds, Dtlink_t* root, int depth, int* level) level[depth] += 1; } -static void dthstat(Dtdata_t* data, Dtstat_t* ds, int* count) -{ +static void dthstat(Dtdata_t *data, Dtstat_t *ds, size_t *count) { Dtlink_t* t; - int n, h; + int h; for(h = data->ntab-1; h >= 0; --h) - { n = 0; + { size_t n = 0; for(t = data->htab[h]; t; t = t->right) n += 1; if(count) @@ -39,8 +37,8 @@ static void dthstat(Dtdata_t* data, Dtstat_t* ds, int* count) int dtstat(Dt_t* dt, Dtstat_t* ds, int all) { - int i; - static int *Count, Size; + static size_t *Count; + static size_t Size; UNFLATTEN(dt); @@ -61,7 +59,7 @@ int dtstat(Dt_t* dt, Dtstat_t* ds, int all) return -1; Size = ds->dt_max+1; } - for(i = ds->dt_max; i >= 0; --i) + for (size_t i = 0; i <= ds->dt_max; ++i) Count[i] = 0; dthstat(dt->data,ds,Count); } @@ -76,10 +74,10 @@ int dtstat(Dt_t* dt, Dtstat_t* ds, int all) Size = ds->dt_n+1; } - for(i = ds->dt_n; i >= 0; --i) + for (size_t i = 0; i <= ds->dt_n; ++i) Count[i] = 0; dttstat(ds,dt->data->here,0,Count); - for(i = ds->dt_n; i >= 0; --i) + for(size_t i = 0; i <= ds->dt_n; ++i) if(Count[i] > ds->dt_max) ds->dt_max = Count[i]; } -- 2.40.0