rewrite XPtrV to use size_t instead of pointer arithmetic, for gcc-snapshot (20120930-1) -fstrict-overflow -Wstrict-overflow=9

This commit is contained in:
tg 2012-10-03 15:50:32 +00:00
parent 167995da22
commit 0575d07671
3 changed files with 35 additions and 31 deletions

15
edit.c
View File

@ -28,7 +28,7 @@
#ifndef MKSH_NO_CMDLINE_EDITING
__RCSID("$MirOS: src/bin/mksh/edit.c,v 1.252 2012/09/21 17:20:20 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/edit.c,v 1.253 2012/10/03 15:50:29 tg Exp $");
/*
* in later versions we might use libtermcap for this, but since external
@ -242,7 +242,7 @@ x_print_expansions(int nwords, char * const *words, bool is_command)
{
bool use_copy = false;
int prefix_len;
XPtrV l = { NULL, NULL, NULL };
XPtrV l = { NULL, 0, 0 };
/*
* Check if all matches are in the same directory (in this
@ -439,7 +439,7 @@ static int
x_command_glob(int flags, char *toglob, char ***wordsp)
{
char *pat, *fpath;
int nwords;
size_t nwords;
XPtrV w;
struct block *l;
@ -470,7 +470,7 @@ x_command_glob(int flags, char *toglob, char ***wordsp)
/* Sort by basename, then path order */
struct path_order_info *info, *last_info = NULL;
char **words = (char **)XPptrv(w);
int i, path_order = 0;
size_t i, path_order = 0;
info = (struct path_order_info *)
alloc2(nwords, sizeof(struct path_order_info), ATEMP);
@ -492,7 +492,7 @@ x_command_glob(int flags, char *toglob, char ***wordsp)
} else {
/* Sort and remove duplicate entries */
char **words = (char **)XPptrv(w);
int i, j;
size_t i, j;
qsort(words, nwords, sizeof(void *), xstrcmp);
for (i = j = 0; i < nwords - 1; i++) {
@ -502,8 +502,7 @@ x_command_glob(int flags, char *toglob, char ***wordsp)
afree(words[i], ATEMP);
}
words[j++] = words[i];
nwords = j;
w.cur = (void **)&words[j];
w.len = nwords = j;
}
XPput(w, NULL);
@ -796,7 +795,7 @@ glob_path(int flags, const char *pat, XPtrV *wp, const char *lpath)
} else
afree(words[i], ATEMP);
}
wp->cur = (void **)&words[j];
wp->len = j;
if (!*sp++)
break;

47
sh.h
View File

@ -157,7 +157,7 @@
#endif
#ifdef EXTERN
__RCSID("$MirOS: src/bin/mksh/sh.h,v 1.588 2012/10/03 15:13:34 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/sh.h,v 1.589 2012/10/03 15:50:31 tg Exp $");
#endif
#define MKSH_VERSION "R40 2012/09/07"
@ -1413,35 +1413,38 @@ char *Xcheck_grow(XString *, const char *, size_t);
* expandable vector of generic pointers
*/
typedef struct XPtrV {
void **cur; /* next avail pointer */
void **beg, **end; /* begin, end of vector */
typedef struct {
/* begin of allocated area */
void **beg;
/* currently used number of entries */
size_t len;
/* allocated number of entries */
size_t siz;
} XPtrV;
#define XPinit(x, n) do { \
void **XPinit_vp; \
XPinit_vp = alloc2((n), sizeof(void *), ATEMP); \
(x).cur = (x).beg = XPinit_vp; \
(x).end = XPinit_vp + (n); \
} while (/* CONSTCOND */ 0)
#define XPinit(x, n) do { \
(x).siz = (n); \
(x).len = 0; \
(x).beg = alloc2((x).siz, sizeof(void *), ATEMP); \
} while (/* CONSTCOND */ 0) \
#define XPput(x, p) do { \
if ((x).cur >= (x).end) { \
size_t n = XPsize(x); \
(x).beg = aresize2((x).beg, \
n, 2 * sizeof(void *), ATEMP); \
(x).cur = (x).beg + n; \
(x).end = (x).cur + n; \
#define XPput(x, p) do { \
if ((x).len == (x).siz) { \
(x).beg = aresize2((x).beg, (x).siz, \
2 * sizeof(void *), ATEMP); \
(x).siz <<= 1; \
} \
*(x).cur++ = (p); \
(x).beg[(x).len++] = (p); \
} while (/* CONSTCOND */ 0)
#define XPptrv(x) ((x).beg)
#define XPsize(x) ((x).cur - (x).beg)
#define XPsize(x) ((x).len)
#define XPclose(x) aresize2((x).beg, XPsize(x), sizeof(void *), ATEMP)
#define XPfree(x) afree((x).beg, ATEMP)
#define IDENT 64
/*
* Lexer internals
*/
typedef struct source Source;
struct source {
@ -1542,10 +1545,12 @@ typedef union {
#define CTRL(x) ((x) == '?' ? 0x7F : (x) & 0x1F) /* ASCII */
#define UNCTRL(x) ((x) ^ 0x40) /* ASCII */
#define IDENT 64
EXTERN Source *source; /* yyparse/yylex source */
EXTERN YYSTYPE yylval; /* result from yylex */
EXTERN struct ioword *heres[HERES], **herep;
EXTERN char ident[IDENT+1];
EXTERN char ident[IDENT + 1];
EXTERN char **history; /* saved commands */
EXTERN char **histptr; /* last history item */

4
syn.c
View File

@ -23,7 +23,7 @@
#include "sh.h"
__RCSID("$MirOS: src/bin/mksh/syn.c,v 1.80 2012/08/17 18:34:25 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/syn.c,v 1.81 2012/10/03 15:50:32 tg Exp $");
extern void yyskiputf8bom(void);
@ -319,7 +319,7 @@ get_command(int cf)
ACCEPT;
/* manipulate the vars string */
tcp = *(--vars.cur);
tcp = XPptrv(vars)[(vars.len = 0)];
/* 'varname=' -> 'varname' */
tcp[wdscan(tcp, EOS) - tcp - 3] = EOS;