Unsign some values related to allocation.

When allocating memory through malloc(9), we always expect the amount of
memory requested to be unsigned as a negative value would either stand for
an error or an overflow.
Unsign some values, found when considering the use of mallocarray(9), to
avoid unnecessary casting. Also consider that indexes should be of
at least the same size/type as the upper limit they pretend to index.

MFC after:	3 weeks
This commit is contained in:
pfg 2018-01-22 02:08:10 +00:00
parent 35335c1009
commit f0c6025eb6
13 changed files with 20 additions and 21 deletions

View File

@ -1198,13 +1198,14 @@ chgetelemstatus(struct cam_periph *periph, int scsi_version, u_long cmd,
struct read_element_status_descriptor *desc; struct read_element_status_descriptor *desc;
caddr_t data = NULL; caddr_t data = NULL;
size_t size, desclen; size_t size, desclen;
int avail, i, error = 0; u_int avail, i;
int curdata, dvcid, sense_flags; int curdata, dvcid, sense_flags;
int try_no_dvcid = 0; int try_no_dvcid = 0;
struct changer_element_status *user_data = NULL; struct changer_element_status *user_data = NULL;
struct ch_softc *softc; struct ch_softc *softc;
union ccb *ccb; union ccb *ccb;
int chet = cesr->cesr_element_type; int chet = cesr->cesr_element_type;
int error = 0;
int want_voltags = (cesr->cesr_flags & CESR_VOLTAGS) ? 1 : 0; int want_voltags = (cesr->cesr_flags & CESR_VOLTAGS) ? 1 : 0;
softc = (struct ch_softc *)periph->softc; softc = (struct ch_softc *)periph->softc;

View File

@ -1164,7 +1164,7 @@ static int
mps_alloc_queues(struct mps_softc *sc) mps_alloc_queues(struct mps_softc *sc)
{ {
struct mps_queue *q; struct mps_queue *q;
int nq, i; u_int nq, i;
nq = sc->msi_msgs; nq = sc->msi_msgs;
mps_dprint(sc, MPS_INIT|MPS_XINFO, "Allocating %d I/O queues\n", nq); mps_dprint(sc, MPS_INIT|MPS_XINFO, "Allocating %d I/O queues\n", nq);

View File

@ -481,7 +481,7 @@ cd9660_readdir(ap)
int error = 0; int error = 0;
int reclen; int reclen;
u_short namelen; u_short namelen;
int ncookies = 0; u_int ncookies = 0;
u_long *cookies = NULL; u_long *cookies = NULL;
cd_ino_t ino; cd_ino_t ino;

View File

@ -561,7 +561,7 @@ nandfs_read_structures(struct nandfs_device *fsdev)
{ {
struct nandfs_fsdata *fsdata, *fsdatat; struct nandfs_fsdata *fsdata, *fsdatat;
struct nandfs_super_block *sblocks, *ssblock; struct nandfs_super_block *sblocks, *ssblock;
int nsbs, nfsds, i; u_int nsbs, nfsds, i;
int error = 0; int error = 0;
int nrsbs; int nrsbs;

View File

@ -2666,7 +2666,7 @@ ncl_flush(struct vnode *vp, int waitfor, struct thread *td,
#define NFS_COMMITBVECSIZ 20 #define NFS_COMMITBVECSIZ 20
#endif #endif
struct buf *bvec_on_stack[NFS_COMMITBVECSIZ]; struct buf *bvec_on_stack[NFS_COMMITBVECSIZ];
int bvecsize = 0, bveccount; u_int bvecsize = 0, bveccount;
if (called_from_renewthread != 0) if (called_from_renewthread != 0)
slptimeo = hz; slptimeo = hz;

View File

@ -1693,7 +1693,7 @@ int
exec_register(const struct execsw *execsw_arg) exec_register(const struct execsw *execsw_arg)
{ {
const struct execsw **es, **xs, **newexecsw; const struct execsw **es, **xs, **newexecsw;
int count = 2; /* New slot and trailing NULL */ u_int count = 2; /* New slot and trailing NULL */
if (execsw) if (execsw)
for (es = execsw; *es; es++) for (es = execsw; *es; es++)

View File

@ -97,10 +97,10 @@ typedef struct elf_file {
Elf_Shdr *e_shdr; Elf_Shdr *e_shdr;
Elf_progent *progtab; Elf_progent *progtab;
int nprogtab; u_int nprogtab;
Elf_relaent *relatab; Elf_relaent *relatab;
int nrelatab; u_int nrelatab;
Elf_relent *reltab; Elf_relent *reltab;
int nreltab; int nreltab;
@ -985,7 +985,7 @@ static void
link_elf_unload_file(linker_file_t file) link_elf_unload_file(linker_file_t file)
{ {
elf_file_t ef = (elf_file_t) file; elf_file_t ef = (elf_file_t) file;
int i; u_int i;
/* Notify MD code that a module is being unloaded. */ /* Notify MD code that a module is being unloaded. */
elf_cpu_unload_file(file); elf_cpu_unload_file(file);

View File

@ -57,9 +57,8 @@ void *
hashinit_flags(int elements, struct malloc_type *type, u_long *hashmask, hashinit_flags(int elements, struct malloc_type *type, u_long *hashmask,
int flags) int flags)
{ {
long hashsize; long hashsize, i;
LIST_HEAD(generic, generic) *hashtbl; LIST_HEAD(generic, generic) *hashtbl;
int i;
KASSERT(elements > 0, ("%s: bad elements", __func__)); KASSERT(elements > 0, ("%s: bad elements", __func__));
/* Exactly one of HASH_WAITOK and HASH_NOWAIT must be set. */ /* Exactly one of HASH_WAITOK and HASH_NOWAIT must be set. */
@ -114,9 +113,8 @@ static const int primes[] = { 1, 13, 31, 61, 127, 251, 509, 761, 1021, 1531,
void * void *
phashinit_flags(int elements, struct malloc_type *type, u_long *nentries, int flags) phashinit_flags(int elements, struct malloc_type *type, u_long *nentries, int flags)
{ {
long hashsize; long hashsize, i;
LIST_HEAD(generic, generic) *hashtbl; LIST_HEAD(generic, generic) *hashtbl;
int i;
KASSERT(elements > 0, ("%s: bad elements", __func__)); KASSERT(elements > 0, ("%s: bad elements", __func__));
/* Exactly one of HASH_WAITOK and HASH_NOWAIT must be set. */ /* Exactly one of HASH_WAITOK and HASH_NOWAIT must be set. */

View File

@ -1554,13 +1554,13 @@ unp_disconnect(struct unpcb *unp, struct unpcb *unp2)
static int static int
unp_pcblist(SYSCTL_HANDLER_ARGS) unp_pcblist(SYSCTL_HANDLER_ARGS)
{ {
int error, i, n;
int freeunp;
struct unpcb *unp, **unp_list; struct unpcb *unp, **unp_list;
unp_gen_t gencnt; unp_gen_t gencnt;
struct xunpgen *xug; struct xunpgen *xug;
struct unp_head *head; struct unp_head *head;
struct xunpcb *xu; struct xunpcb *xu;
u_int i;
int freeunp, error, n;
switch ((intptr_t)arg1) { switch ((intptr_t)arg1) {
case SOCK_STREAM: case SOCK_STREAM:

View File

@ -355,7 +355,7 @@ get_map(struct ip_fw_chain *chain, int extra, int locked)
for (;;) { for (;;) {
struct ip_fw **map; struct ip_fw **map;
int i, mflags; u_int i, mflags;
mflags = M_ZERO | ((locked != 0) ? M_NOWAIT : M_WAITOK); mflags = M_ZERO | ((locked != 0) ? M_NOWAIT : M_WAITOK);

View File

@ -3266,10 +3266,10 @@ static int
ta_init_fhash(struct ip_fw_chain *ch, void **ta_state, struct table_info *ti, ta_init_fhash(struct ip_fw_chain *ch, void **ta_state, struct table_info *ti,
char *data, uint8_t tflags) char *data, uint8_t tflags)
{ {
int i;
struct fhash_cfg *cfg; struct fhash_cfg *cfg;
struct fhashentry4 *fe4; struct fhashentry4 *fe4;
struct fhashentry6 *fe6; struct fhashentry6 *fe6;
u_int i;
cfg = malloc(sizeof(struct fhash_cfg), M_IPFW, M_WAITOK | M_ZERO); cfg = malloc(sizeof(struct fhash_cfg), M_IPFW, M_WAITOK | M_ZERO);
@ -3672,7 +3672,7 @@ ta_prepare_mod_fhash(void *ta_buf, uint64_t *pflags)
{ {
struct mod_item *mi; struct mod_item *mi;
struct fhashbhead *head; struct fhashbhead *head;
int i; u_int i;
mi = (struct mod_item *)ta_buf; mi = (struct mod_item *)ta_buf;

View File

@ -118,7 +118,7 @@ smb_ntencrypt(const u_char *apwd, u_char *C8, u_char *RN)
u_char S21[21]; u_char S21[21];
u_int16_t *unipwd; u_int16_t *unipwd;
MD4_CTX *ctxp; MD4_CTX *ctxp;
int len; u_int len;
len = strlen(apwd); len = strlen(apwd);
unipwd = malloc((len + 1) * sizeof(u_int16_t), M_SMBTEMP, M_WAITOK); unipwd = malloc((len + 1) * sizeof(u_int16_t), M_SMBTEMP, M_WAITOK);
@ -148,7 +148,7 @@ smb_calcmackey(struct smb_vc *vcp)
{ {
const char *pwd; const char *pwd;
u_int16_t *unipwd; u_int16_t *unipwd;
int len; u_int len;
MD4_CTX md4; MD4_CTX md4;
u_char S16[16], S21[21]; u_char S16[16], S21[21];

View File

@ -841,7 +841,7 @@ cmci_setup(void)
static void static void
amd_thresholding_setup(void) amd_thresholding_setup(void)
{ {
int i; u_int i;
amd_et_state = malloc((mp_maxid + 1) * sizeof(struct amd_et_state *), amd_et_state = malloc((mp_maxid + 1) * sizeof(struct amd_et_state *),
M_MCA, M_WAITOK); M_MCA, M_WAITOK);