Skip to content

Commit

Permalink
Use size_t where appropriate.
Browse files Browse the repository at this point in the history
gcc -Wconversion warning output is reduced from 975 lines to 679 lines.
  • Loading branch information
gwsw committed Nov 14, 2023
1 parent 8b71b29 commit b469710
Show file tree
Hide file tree
Showing 31 changed files with 257 additions and 249 deletions.
34 changes: 20 additions & 14 deletions ch.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ struct bufnode {
struct buf {
struct bufnode node;
BLOCKNUM block;
unsigned int datasize;
size_t datasize;
unsigned char data[LBUFSIZE];
};
#define bufnode_buf(bn) ((struct buf *) bn)
Expand All @@ -64,7 +64,7 @@ struct filestate {
POSITION fpos;
int nbufs;
BLOCKNUM block;
unsigned int offset;
size_t offset;
POSITION fsize;
};

Expand Down Expand Up @@ -139,6 +139,13 @@ extern char *namelogfile;

static int ch_addbuf();

/*
* Return the file position corresponding to an offset within a block.
*/
static POSITION ch_position(BLOCKNUM block, size_t offset)
{
return (ch_block * LBUFSIZE) + (POSITION) offset;
}

/*
* Get the character pointed to by the read pointer.
Expand All @@ -147,7 +154,7 @@ static int ch_get(void)
{
struct buf *bp;
struct bufnode *bn;
int n;
ssize_t n;
int read_again;
int h;
POSITION pos;
Expand Down Expand Up @@ -223,7 +230,7 @@ static int ch_get(void)

for (;;)
{
pos = (ch_block * LBUFSIZE) + bp->datasize;
pos = ch_position(ch_block, bp->datasize);
if ((len = ch_length()) != NULL_POSITION && pos >= len)
/*
* At end of file.
Expand Down Expand Up @@ -264,8 +271,7 @@ static int ch_get(void)
n = 1;
} else
{
n = iread(ch_file, &bp->data[bp->datasize],
(unsigned int)(LBUFSIZE - bp->datasize));
n = iread(ch_file, &bp->data[bp->datasize], LBUFSIZE - bp->datasize);
}

read_again = FALSE;
Expand Down Expand Up @@ -298,12 +304,12 @@ static int ch_get(void)
if (secure_allow(SF_LOGFILE))
{
if (logfile >= 0 && n > 0)
write(logfile, (char *) &bp->data[bp->datasize], n);
write(logfile, (char *) &bp->data[bp->datasize], (size_t) n);
}
#endif

ch_fpos += n;
bp->datasize += n;
bp->datasize += (size_t) n;

if (n == 0)
{
Expand Down Expand Up @@ -489,7 +495,7 @@ public int ch_seek(POSITION pos)
* Set read pointer.
*/
ch_block = new_block;
ch_offset = pos % LBUFSIZE;
ch_offset = (size_t) (pos % LBUFSIZE);
return (0);
}

Expand Down Expand Up @@ -536,7 +542,7 @@ public int ch_end_buffer_seek(void)
FOR_BUFS(bn)
{
bp = bufnode_buf(bn);
buf_pos = (bp->block * LBUFSIZE) + bp->datasize;
buf_pos = ch_position(bp->block, bp->datasize);
if (buf_pos > end_pos)
end_pos = buf_pos;
}
Expand Down Expand Up @@ -600,7 +606,7 @@ public POSITION ch_tell(void)
{
if (thisfile == NULL)
return (NULL_POSITION);
return (ch_block * LBUFSIZE) + ch_offset;
return ch_position(ch_block, ch_offset);
}

/*
Expand Down Expand Up @@ -650,14 +656,14 @@ public int ch_back_get(void)
* Set max amount of buffer space.
* bufspace is in units of 1024 bytes. -1 mean no limit.
*/
public void ch_setbufspace(int bufspace)
public void ch_setbufspace(size_t bufspace)
{
if (bufspace < 0)
maxbufs = -1;
else
{
int lbufk = LBUFSIZE / 1024;
maxbufs = bufspace / lbufk + (bufspace % lbufk != 0);
size_t lbufk = LBUFSIZE / 1024;
maxbufs = (int) (bufspace / lbufk + (bufspace % lbufk != 0));
if (maxbufs < 1)
maxbufs = 1;
}
Expand Down
8 changes: 4 additions & 4 deletions charset.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ static struct wchar_range_table user_prt_table;
static void wchar_range_table_set(struct wchar_range_table *tbl, struct xbuffer *arr)
{
tbl->table = (struct wchar_range *) arr->data;
tbl->count = arr->end / sizeof(struct wchar_range);
tbl->count = (unsigned int) (arr->end / sizeof(struct wchar_range));
}

/*
Expand Down Expand Up @@ -845,8 +845,8 @@ static struct wchar_range comb_table[] = {

static int is_in_table(LWCHAR ch, struct wchar_range_table *table)
{
int hi;
int lo;
unsigned int hi;
unsigned int lo;

/* Binary search in the table. */
if (table->table == NULL || table->count == 0 || ch < table->table[0].first)
Expand All @@ -855,7 +855,7 @@ static int is_in_table(LWCHAR ch, struct wchar_range_table *table)
hi = table->count - 1;
while (lo <= hi)
{
int mid = (lo + hi) / 2;
unsigned int mid = (lo + hi) / 2;
if (ch > table->table[mid].last)
lo = mid + 1;
else if (ch < table->table[mid].first)
Expand Down
24 changes: 11 additions & 13 deletions cmdbuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ static int prompt_col; /* Column of cursor just after prompt */
static char *cp; /* Pointer into cmdbuf */
static int cmd_offset; /* Index into cmdbuf of first displayed char */
static int literal; /* Next input char should not be interpreted */
public int updown_match = -1; /* Prefix length in up/down movement */
public ssize_t updown_match = -1; /* Prefix length in up/down movement */

#if TAB_COMPLETE_FILENAME
static int cmd_complete(int action);
Expand Down Expand Up @@ -186,7 +186,7 @@ public int len_cmdbuf(void)
* {{ Returning pwidth and bswidth separately is a historical artifact
* since they're always the same. Maybe clean this up someday. }}
*/
static constant char * cmd_step_common(char *p, LWCHAR ch, int len, int *pwidth, int *bswidth)
static constant char * cmd_step_common(char *p, LWCHAR ch, size_t len, int *pwidth, int *bswidth)
{
constant char *pr;
int width;
Expand Down Expand Up @@ -226,7 +226,7 @@ static constant char * cmd_step_right(char **pp, int *pwidth, int *bswidth)
char *p = *pp;
LWCHAR ch = step_char(pp, +1, p + strlen(p));

return cmd_step_common(p, ch, *pp - p, pwidth, bswidth);
return cmd_step_common(p, ch, ptr_diff(*pp, p), pwidth, bswidth);
}

/*
Expand All @@ -237,7 +237,7 @@ static constant char * cmd_step_left(char **pp, int *pwidth, int *bswidth)
char *p = *pp;
LWCHAR ch = step_char(pp, -1, cmdbuf);

return cmd_step_common(*pp, ch, p - *pp, pwidth, bswidth);
return cmd_step_common(*pp, ch, ptr_diff(p, *pp), pwidth, bswidth);
}

/*
Expand Down Expand Up @@ -436,7 +436,7 @@ static int cmd_left(void)
/*
* Insert a char into the command buffer, at the current position.
*/
static int cmd_ichar(constant char *cs, int clen)
static int cmd_ichar(constant char *cs, size_t clen)
{
char *s;

Expand Down Expand Up @@ -644,9 +644,7 @@ static int cmd_updown(int action)
}

if (updown_match < 0)
{
updown_match = (int) (cp - cmdbuf);
}
updown_match = ptr_diff(cp, cmdbuf);

/*
* Find the next history entry which matches.
Expand Down Expand Up @@ -911,7 +909,7 @@ static int cmd_istr(constant char *str)
{
constant char *os = s;
step_charc(&s, +1, endline);
action = cmd_ichar(os, s - os);
action = cmd_ichar(os, ptr_diff(s, os));
if (action != CC_OK)
return (action);
}
Expand All @@ -932,7 +930,7 @@ static char * delimit_word(void)
int delim_quoted = 0;
int meta_quoted = 0;
constant char *esc = get_meta_escape();
int esclen = (int) strlen(esc);
size_t esclen = strlen(esc);
#endif

/*
Expand Down Expand Up @@ -1039,8 +1037,8 @@ static void init_compl(void)
*/
if (tk_original != NULL)
free(tk_original);
tk_original = (char *) ecalloc(cp-word+1, sizeof(char));
strncpy(tk_original, word, cp-word);
tk_original = (char *) ecalloc(ptr_diff(cp,word)+1, sizeof(char));
strncpy(tk_original, word, ptr_diff(cp,word));
/*
* Get the expanded filename.
* This may result in a single filename, or
Expand Down Expand Up @@ -1190,7 +1188,7 @@ static int cmd_complete(int action)
public int cmd_char(int c)
{
int action;
int len;
size_t len;

if (!utf_mode)
{
Expand Down
8 changes: 4 additions & 4 deletions command.c
Original file line number Diff line number Diff line change
Expand Up @@ -2047,13 +2047,13 @@ public void commands(void)
* Shift view left.
*/
if (number > 0)
shift_count = number;
shift_count = (int) number;
else
number = (shift_count > 0) ? shift_count : sc_width / 2;
if (number > hshift)
number = hshift;
pos_rehead();
hshift -= number;
hshift -= (int) number;
screen_trashed();
break;

Expand All @@ -2062,11 +2062,11 @@ public void commands(void)
* Shift view right.
*/
if (number > 0)
shift_count = number;
shift_count = (int) number;
else
number = (shift_count > 0) ? shift_count : sc_width / 2;
pos_rehead();
hshift += number;
hshift += (int) number;
screen_trashed();
break;

Expand Down
16 changes: 8 additions & 8 deletions cvt.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ extern int utf_mode;
/*
* Get the length of a buffer needed to convert a string.
*/
public int cvt_length(int len, int ops)
public size_t cvt_length(size_t len, int ops)
{
if (utf_mode)
/*
Expand All @@ -34,10 +34,10 @@ public int cvt_length(int len, int ops)
/*
* Allocate a chpos array for use by cvt_text.
*/
public int * cvt_alloc_chpos(int len)
public int * cvt_alloc_chpos(size_t len)
{
int i;
int *chpos = (int *) ecalloc(sizeof(int), len);
int *chpos = (int *) ecalloc(len, sizeof(int));
/* Initialize all entries to an invalid position. */
for (i = 0; i < len; i++)
chpos[i] = -1;
Expand All @@ -49,7 +49,7 @@ public int * cvt_alloc_chpos(int len)
* Returns converted text in odst. The original offset of each
* odst character (when it was in osrc) is returned in the chpos array.
*/
public void cvt_text(char *odst, constant char *osrc, int *chpos, int *lenp, int ops)
public void cvt_text(char *odst, constant char *osrc, int *chpos, size_t *lenp, int ops)
{
char *dst;
char *edst = odst;
Expand All @@ -64,8 +64,8 @@ public void cvt_text(char *odst, constant char *osrc, int *chpos, int *lenp, int

for (src = osrc, dst = odst; src < src_end; )
{
int src_pos = (int) (src - osrc);
int dst_pos = (int) (dst - odst);
size_t src_pos = ptr_diff(src, osrc);
size_t dst_pos = ptr_diff(dst, odst);
struct ansi_state *pansi;
ch = step_charc(&src, +1, src_end);
if ((ops & CVT_BS) && ch == '\b' && dst > odst)
Expand Down Expand Up @@ -96,7 +96,7 @@ public void cvt_text(char *odst, constant char *osrc, int *chpos, int *lenp, int
if (chpos != NULL)
{
while (cdst++ < dst)
chpos[dst_pos++] = src_pos;
chpos[dst_pos++] = (int) src_pos; /*{{type-issue}}*/
}
}
if (dst > edst)
Expand All @@ -106,6 +106,6 @@ public void cvt_text(char *odst, constant char *osrc, int *chpos, int *lenp, int
edst--;
*edst = '\0';
if (lenp != NULL)
*lenp = (int) (edst - odst);
*lenp = ptr_diff(edst, odst);
/* FIXME: why was this here? if (chpos != NULL) chpos[dst - odst] = src - osrc; */
}

0 comments on commit b469710

Please sign in to comment.