Re: use mallocarray in kern

2014-07-14 Thread Henri Kemppainen
If you look at the diff that went in already, it's using mallocarray.
It wouldn't even compile otherwise.



Re: use mallocarray in kern

2014-07-14 Thread Philipp
Sorry to break the threading, but I already expunged the original 
message..


Re: http://marc.info/?l=openbsd-tech&m=140529530814733&w=2

The second and third hunk should use mallocarray() instead of malloc() 
in my eyes.

sizeof(Elf_Phdr) as type just doesnt make sense to me.

Hope not "everyone" is on the plane right now.

--double-p



Re: use mallocarray in kern

2014-07-13 Thread Theo de Raadt
> On Sun, Jul 13, 2014 at 17:52, Theo de Raadt wrote:
> >> And some others from exec that follow a slightly different pattern.
> > 
> > This is the pattern I have been following as well.
> > 
> > As for the final diff, I've been giving up on the "known constant"
> > scenario.  It seems expensive.
> 
> Meh. :) I think they can be changed back if necessary; in the mean
> time it makes it easier to see what's done and what remains.

It is an extra register window on sparc and sparc64.



Re: use mallocarray in kern

2014-07-13 Thread Ted Unangst
On Sun, Jul 13, 2014 at 17:52, Theo de Raadt wrote:
>> And some others from exec that follow a slightly different pattern.
> 
> This is the pattern I have been following as well.
> 
> As for the final diff, I've been giving up on the "known constant"
> scenario.  It seems expensive.

Meh. :) I think they can be changed back if necessary; in the mean
time it makes it easier to see what's done and what remains.



Re: use mallocarray in kern

2014-07-13 Thread Theo de Raadt
> And some others from exec that follow a slightly different pattern.

This is the pattern I have been following as well.

As for the final diff, I've been giving up on the "known constant"
scenario.  It seems expensive.

> Index: exec_script.c
> ===
> RCS file: /cvs/src/sys/kern/exec_script.c,v
> retrieving revision 1.30
> diff -u -p -r1.30 exec_script.c
> --- exec_script.c 12 Jul 2014 18:43:32 -  1.30
> +++ exec_script.c 13 Jul 2014 23:46:23 -
> @@ -208,7 +208,7 @@ check_shell:
>   epp->ep_flags |= EXEC_INDIR;
>  
>   /* and set up the fake args list, for later */
> - shellargp = malloc(4 * sizeof(char *), M_EXEC, M_WAITOK);
> + shellargp = mallocarray(4, sizeof(char *), M_EXEC, M_WAITOK);
>   tmpsap = shellargp;
>   *tmpsap = malloc(shellnamelen + 1, M_EXEC, M_WAITOK);
>   strlcpy(*tmpsap++, shellname, shellnamelen + 1);
> 



Re: use mallocarray in kern

2014-07-13 Thread Ted Unangst
On Sun, Jul 13, 2014 at 11:14, Ted Unangst wrote:
> sprinkle malloc array in the kern directory.

And some others from exec that follow a slightly different pattern.

Index: exec_elf.c
===
RCS file: /cvs/src/sys/kern/exec_elf.c,v
retrieving revision 1.99
diff -u -p -r1.99 exec_elf.c
--- exec_elf.c  12 Jul 2014 18:43:32 -  1.99
+++ exec_elf.c  13 Jul 2014 23:46:23 -
@@ -355,8 +355,8 @@ ELFNAME(load_file)(struct proc *p, char 
goto bad1;
}
 
+   ph = mallocarray(eh.e_phnum, sizeof(Elf_Phdr), M_TEMP, M_WAITOK);
phsize = eh.e_phnum * sizeof(Elf_Phdr);
-   ph = malloc(phsize, M_TEMP, M_WAITOK);
 
if ((error = ELFNAME(read_from)(p, nd.ni_vp, eh.e_phoff, (caddr_t)ph,
phsize)) != 0)
@@ -539,8 +539,8 @@ ELFNAME2(exec,makecmds)(struct proc *p, 
 * Allocate space to hold all the program headers, and read them
 * from the file
 */
+   ph = malloc(eh->e_phnum, sizeof(Elf_Phdr), M_TEMP, M_WAITOK);
phsize = eh->e_phnum * sizeof(Elf_Phdr);
-   ph = malloc(phsize, M_TEMP, M_WAITOK);
 
if ((error = ELFNAME(read_from)(p, epp->ep_vp, eh->e_phoff, (caddr_t)ph,
phsize)) != 0)
@@ -860,8 +860,8 @@ ELFNAME(os_pt_note)(struct proc *p, stru
size_t phsize;
int error;
 
+   hph = malloc(eh->e_phnum, sizeof(Elf_Phdr), M_TEMP, M_WAITOK);
phsize = eh->e_phnum * sizeof(Elf_Phdr);
-   hph = malloc(phsize, M_TEMP, M_WAITOK);
if ((error = ELFNAME(read_from)(p, epp->ep_vp, eh->e_phoff,
(caddr_t)hph, phsize)) != 0)
goto out1;
@@ -1005,7 +1005,7 @@ ELFNAMEEND(coredump)(struct proc *p, voi
notestart = offset + sizeof(phdr) * cs.npsections;
secstart = notestart + notesize;
 
-   psections = malloc(cs.npsections * sizeof(Elf_Phdr),
+   psections = mallocarray(cs.npsections, sizeof(Elf_Phdr),
M_TEMP, M_WAITOK|M_ZERO);
 
/* Pass 2: now write the P-section headers. */
Index: exec_script.c
===
RCS file: /cvs/src/sys/kern/exec_script.c,v
retrieving revision 1.30
diff -u -p -r1.30 exec_script.c
--- exec_script.c   12 Jul 2014 18:43:32 -  1.30
+++ exec_script.c   13 Jul 2014 23:46:23 -
@@ -208,7 +208,7 @@ check_shell:
epp->ep_flags |= EXEC_INDIR;
 
/* and set up the fake args list, for later */
-   shellargp = malloc(4 * sizeof(char *), M_EXEC, M_WAITOK);
+   shellargp = mallocarray(4, sizeof(char *), M_EXEC, M_WAITOK);
tmpsap = shellargp;
*tmpsap = malloc(shellnamelen + 1, M_EXEC, M_WAITOK);
strlcpy(*tmpsap++, shellname, shellnamelen + 1);



use mallocarray in kern

2014-07-13 Thread Ted Unangst
sprinkle malloc array in the kern directory.

Index: exec_subr.c
===
RCS file: /cvs/src/sys/kern/exec_subr.c,v
retrieving revision 1.36
diff -u -p -r1.36 exec_subr.c
--- exec_subr.c 12 Jul 2014 18:43:32 -  1.36
+++ exec_subr.c 13 Jul 2014 15:13:59 -
@@ -91,7 +91,7 @@ vmcmdset_extend(struct exec_vmcmd_set *e
evsp->evs_cnt += ocnt;
 
/* reallocate the command set */
-   nvcp = malloc(evsp->evs_cnt * sizeof(struct exec_vmcmd), M_EXEC,
+   nvcp = mallocarray(evsp->evs_cnt, sizeof(struct exec_vmcmd), M_EXEC,
M_WAITOK);
bcopy(evsp->evs_cmds, nvcp, (ocnt * sizeof(struct exec_vmcmd)));
if (evsp->evs_cmds != evsp->evs_start)
Index: kern_descrip.c
===
RCS file: /cvs/src/sys/kern/kern_descrip.c,v
retrieving revision 1.111
diff -u -p -r1.111 kern_descrip.c
--- kern_descrip.c  12 Jul 2014 18:43:32 -  1.111
+++ kern_descrip.c  13 Jul 2014 15:14:00 -
@@ -765,7 +765,7 @@ fdexpand(struct proc *p)
else
nfiles = 2 * fdp->fd_nfiles;
 
-   newofile = malloc(nfiles * OFILESIZE, M_FILEDESC, M_WAITOK);
+   newofile = mallocarray(nfiles, OFILESIZE, M_FILEDESC, M_WAITOK);
newofileflags = (char *) &newofile[nfiles];
 
/*
@@ -784,9 +784,9 @@ fdexpand(struct proc *p)
free(fdp->fd_ofiles, M_FILEDESC, 0);
 
if (NDHISLOTS(nfiles) > NDHISLOTS(fdp->fd_nfiles)) {
-   newhimap = malloc(NDHISLOTS(nfiles) * sizeof(u_int),
+   newhimap = mallocarray(NDHISLOTS(nfiles), sizeof(u_int),
M_FILEDESC, M_WAITOK);
-   newlomap = malloc(NDLOSLOTS(nfiles) * sizeof(u_int),
+   newlomap = mallocarray(NDLOSLOTS(nfiles), sizeof(u_int),
M_FILEDESC, M_WAITOK);
 
copylen = NDHISLOTS(fdp->fd_nfiles) * sizeof(u_int);
@@ -939,7 +939,7 @@ fdcopy(struct process *pr)
i = newfdp->fd_nfiles;
while (i >= 2 * NDEXTENT && i > newfdp->fd_lastfile * 2)
i /= 2;
-   newfdp->fd_ofiles = malloc(i * OFILESIZE, M_FILEDESC, M_WAITOK);
+   newfdp->fd_ofiles = mallocarray(i, OFILESIZE, M_FILEDESC, 
M_WAITOK);
newfdp->fd_ofileflags = (char *) &newfdp->fd_ofiles[i];
}
if (NDHISLOTS(i) <= NDHISLOTS(NDFILE)) {
@@ -948,9 +948,9 @@ fdcopy(struct process *pr)
newfdp->fd_lomap =
((struct filedesc0 *) newfdp)->fd_dlomap;
} else {
-   newfdp->fd_himap = malloc(NDHISLOTS(i) * sizeof(u_int),
+   newfdp->fd_himap = mallocarray(NDHISLOTS(i), sizeof(u_int),
M_FILEDESC, M_WAITOK);
-   newfdp->fd_lomap = malloc(NDLOSLOTS(i) * sizeof(u_int),
+   newfdp->fd_lomap = mallocarray(NDLOSLOTS(i), sizeof(u_int),
M_FILEDESC, M_WAITOK);
}
newfdp->fd_nfiles = i;
Index: kern_subr.c
===
RCS file: /cvs/src/sys/kern/kern_subr.c,v
retrieving revision 1.38
diff -u -p -r1.38 kern_subr.c
--- kern_subr.c 12 Jul 2014 18:43:32 -  1.38
+++ kern_subr.c 13 Jul 2014 15:14:00 -
@@ -166,7 +166,7 @@ hashinit(int elements, int type, int fla
panic("hashinit: bad cnt");
for (hashsize = 1; hashsize < elements; hashsize <<= 1)
continue;
-   hashtbl = malloc(hashsize * sizeof(*hashtbl), type, flags);
+   hashtbl = mallocarray(hashsize, sizeof(*hashtbl), type, flags);
if (hashtbl == NULL)
return NULL;
for (i = 0; i < hashsize; i++)
Index: kern_sysctl.c
===
RCS file: /cvs/src/sys/kern/kern_sysctl.c,v
retrieving revision 1.256
diff -u -p -r1.256 kern_sysctl.c
--- kern_sysctl.c   12 Jul 2014 18:43:32 -  1.256
+++ kern_sysctl.c   13 Jul 2014 15:14:00 -
@@ -1854,7 +1854,7 @@ sysctl_diskinit(int update, struct proc 
free(diskstats, M_SYSCTL, 0);
diskstats = NULL;
disknames = NULL;
-   diskstats = malloc(disk_count * sizeof(struct diskstats),
+   diskstats = mallocarray(disk_count, sizeof(struct diskstats),
M_SYSCTL, M_WAITOK);
disknames = malloc(tlen, M_SYSCTL, M_WAITOK);
disknames[0] = '\0';
Index: subr_autoconf.c
===
RCS file: /cvs/src/sys/kern/subr_autoconf.c,v
retrieving revision 1.77
diff -u -p -r1.77 subr_autoconf.c
--- subr_autoconf.c 12 Jul 2014 18:43:32 -  1.77
+++ subr_autoconf.c 13 Jul 2014 15:14:00 -
@@ -462,7 +462,7 @@ config_make_softc(struct device *parent,
while (new <= dev->dv_unit)
new *= 2;