From: Ben Gras Date: Mon, 16 Jul 2012 11:17:11 +0000 (+0200) Subject: various coverity-inspired fixes X-Git-Tag: v3.2.1~451 X-Git-Url: http://zhaoyanbai.com/repos/%22http:/www.isc.org/icons/doc/syslog.3.html?a=commitdiff_plain;h=cbcdb838f167dc51956da5dae9dc9fb78869d87a;p=minix.git various coverity-inspired fixes . some strncpy/strcpy to strlcpy conversions . new to avoid including other minix headers that have colliding definitions with library and commands code, causing parse warnings . removed some dead code / assignments --- diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c index 09e64b91c..93551f31e 100644 --- a/drivers/pci/pci.c +++ b/drivers/pci/pci.c @@ -12,6 +12,7 @@ Created: Jan 2000 by Philip Homburg #include #include #include +#include #include "pci.h" #include diff --git a/include/minix/Makefile b/include/minix/Makefile index ab93226a4..c876227f7 100644 --- a/include/minix/Makefile +++ b/include/minix/Makefile @@ -4,7 +4,7 @@ NOOBJ= # defined INCSDIR= /usr/include/minix -INCS+= dirent.h paths.h types.h +INCS+= dirent.h paths.h types.h param.h .include "${NETBSDSRCDIR}/common/include/sys/Makefile.mount.inc" .include "${NETBSDSRCDIR}/common/include/Makefile.termios.inc" diff --git a/include/minix/const.h b/include/minix/const.h index b52f94e94..ed69a300e 100644 --- a/include/minix/const.h +++ b/include/minix/const.h @@ -72,12 +72,6 @@ #define FUTURE_CODE 0 /* new code to be activated + tested later */ #define TEMP_CODE 1 /* active code to be removed later */ -/* Number of processes contained in the system image. */ -#define NR_BOOT_PROCS (NR_TASKS + LAST_SPECIAL_PROC_NR + 1) - -/* Process name length in the PM process table, including '\0'. */ -#define PROC_NAME_LEN 16 - /* Miscellaneous */ #define BYTE 0377 /* mask for 8 bits */ #define READING 0 /* copy data to user */ diff --git a/include/minix/param.h b/include/minix/param.h new file mode 100644 index 000000000..b4abbfc9c --- /dev/null +++ b/include/minix/param.h @@ -0,0 +1,45 @@ + +#ifndef _MINIX_PARAM_H +#define _MINIX_PARAM_H 1 + +#include +#include + +/* Number of processes contained in the system image. */ +#define NR_BOOT_PROCS (NR_TASKS + LAST_SPECIAL_PROC_NR + 1) + +/* This is used to obtain system information through SYS_GETINFO. */ +#define MAXMEMMAP 40 +typedef struct kinfo { + /* Straight multiboot-provided info */ + multiboot_info_t mbi; + multiboot_module_t module_list[MULTIBOOT_MAX_MODS]; + multiboot_memory_map_t memmap[MAXMEMMAP]; /* free mem list */ + phys_bytes mem_high_phys; + int mmap_size; + + /* Multiboot-derived */ + int mods_with_kernel; /* no. of mods incl kernel */ + int kern_mod; /* which one is kernel */ + + /* Minix stuff, started at bootstrap phase */ + int freepde_start; /* lowest pde unused kernel pde */ + char param_buf[MULTIBOOT_PARAM_BUF_SIZE]; + + /* Minix stuff */ + struct kmessages *kmess; + int do_serial_debug; /* system serial output */ + int serial_debug_baud; /* serial baud rate */ + int minix_panicing; /* are we panicing? */ + vir_bytes user_sp; /* where does kernel want stack set */ + vir_bytes user_end; /* upper proc limit */ + vir_bytes vir_kern_start; /* kernel addrspace starts */ + vir_bytes bootstrap_start, bootstrap_len; + struct boot_image boot_procs[NR_BOOT_PROCS]; + int nr_procs; /* number of user processes */ + int nr_tasks; /* number of kernel tasks */ + char release[6]; /* kernel release number */ + char version[6]; /* kernel version number */ +} kinfo_t; + +#endif diff --git a/include/minix/type.h b/include/minix/type.h index 094cd50c6..c8ee62b26 100644 --- a/include/minix/type.h +++ b/include/minix/type.h @@ -10,9 +10,6 @@ #include #endif -#include -#include - #include /* Type definitions. */ @@ -122,6 +119,8 @@ struct mem_range phys_bytes mr_limit; /* Highest memory address in range */ }; +#define PROC_NAME_LEN 16 + /* List of boot-time processes set in kernel/table.c. */ struct boot_image { int proc_nr; /* process number to use */ @@ -137,40 +136,6 @@ struct memory { phys_bytes size; }; -/* This is used to obtain system information through SYS_GETINFO. */ -#define MAXMEMMAP 40 -typedef struct kinfo { - /* Straight multiboot-provided info */ - multiboot_info_t mbi; - multiboot_module_t module_list[MULTIBOOT_MAX_MODS]; - multiboot_memory_map_t memmap[MAXMEMMAP]; /* free mem list */ - phys_bytes mem_high_phys; - int mmap_size; - - /* Multiboot-derived */ - int mods_with_kernel; /* no. of mods incl kernel */ - int kern_mod; /* which one is kernel */ - - /* Minix stuff, started at bootstrap phase */ - int freepde_start; /* lowest pde unused kernel pde */ - char param_buf[MULTIBOOT_PARAM_BUF_SIZE]; - - /* Minix stuff */ - struct kmessages *kmess; - int do_serial_debug; /* system serial output */ - int serial_debug_baud; /* serial baud rate */ - int minix_panicing; /* are we panicing? */ - vir_bytes user_sp; /* where does kernel want stack set */ - vir_bytes user_end; /* upper proc limit */ - vir_bytes vir_kern_start; /* kernel addrspace starts */ - vir_bytes bootstrap_start, bootstrap_len; - struct boot_image boot_procs[NR_BOOT_PROCS]; - int nr_procs; /* number of user processes */ - int nr_tasks; /* number of kernel tasks */ - char release[6]; /* kernel release number */ - char version[6]; /* kernel version number */ -} kinfo_t; - #define STATICINIT(v, n) \ if(!(v)) { \ if(!((v) = alloc_contig(sizeof(*(v)) * (n), 0, NULL))) { \ diff --git a/kernel/arch/i386/memory.c b/kernel/arch/i386/memory.c index e87628103..f4033c4f0 100644 --- a/kernel/arch/i386/memory.c +++ b/kernel/arch/i386/memory.c @@ -746,7 +746,7 @@ void memory_init(void) void arch_proc_init(struct proc *pr, const u32_t ip, const u32_t sp, char *name) { arch_proc_reset(pr); - strcpy(pr->p_name, name); + strlcpy(pr->p_name, name, sizeof(pr->p_name)); /* set custom state we know */ pr->p_reg.pc = ip; diff --git a/kernel/arch/i386/pg_utils.c b/kernel/arch/i386/pg_utils.c index f096369a0..1791a41c4 100644 --- a/kernel/arch/i386/pg_utils.c +++ b/kernel/arch/i386/pg_utils.c @@ -1,15 +1,13 @@ #include -#include -#include #include #include "kernel.h" +#include #include "arch_proto.h" #include #include -#include /* These are set/computed in kernel.lds. */ extern char _kern_vir_base, _kern_phys_base, _kern_size; @@ -174,11 +172,11 @@ void pg_identity(kinfo_t *cbi) for(i = 0; i < I386_VM_DIR_ENTRIES; i++) { u32_t flags = I386_VM_PRESENT | I386_VM_BIGPAGE | I386_VM_USER | I386_VM_WRITE; + phys = i * I386_BIG_PAGE_SIZE; if((cbi->mem_high_phys & I386_VM_ADDR_MASK_4MB) <= (phys & I386_VM_ADDR_MASK_4MB)) { flags |= I386_VM_PWT | I386_VM_PCD; } - phys = i * I386_BIG_PAGE_SIZE; pagedir[i] = phys | flags; } } diff --git a/kernel/arch/i386/protect.c b/kernel/arch/i386/protect.c index 6627cc329..9fcfb4c26 100644 --- a/kernel/arch/i386/protect.c +++ b/kernel/arch/i386/protect.c @@ -364,7 +364,7 @@ void arch_boot_proc(struct boot_image *ip, struct proc *rp) execi.proc_e = ip->endpoint; execi.hdr = (char *) mod->mod_start; /* phys mem direct */ execi.hdr_len = mod->mod_end - mod->mod_start; - strcpy(execi.progname, ip->proc_name); + strlcpy(execi.progname, ip->proc_name, sizeof(execi.progname)); execi.frame_len = 0; /* callbacks for use in the kernel */ diff --git a/kernel/kernel.h b/kernel/kernel.h index 7a6a148d4..48aa3106c 100644 --- a/kernel/kernel.h +++ b/kernel/kernel.h @@ -34,6 +34,7 @@ #include /* watchdog timer management */ #include /* return codes and error numbers */ #include +#include /* Important kernel header files. */ #include "config.h" /* configuration, MUST be first */ diff --git a/kernel/main.c b/kernel/main.c index 7d92ce437..c1448833e 100644 --- a/kernel/main.c +++ b/kernel/main.c @@ -201,7 +201,8 @@ void kmain(kinfo_t *local_cbi) kcalls = TSK_KC; /* allowed kernel calls */ } /* Priviliges for the root system process. */ - else if(isrootsysn(proc_nr)) { + else { + assert(isrootsysn(proc_nr)); priv(rp)->s_flags= RSYS_F; /* privilege flags */ priv(rp)->s_trap_mask= SRV_T; /* allowed traps */ ipc_to_m = SRV_M; /* allowed targets */ @@ -210,10 +211,6 @@ void kmain(kinfo_t *local_cbi) rp->p_priority = SRV_Q; /* priority queue */ rp->p_quantum_size_ms = SRV_QT; /* quantum size */ } - /* Priviliges for ordinary process. */ - else { - NOT_REACHABLE; - } /* Fill in target mask. */ memset(&map, 0, sizeof(map)); @@ -394,10 +391,8 @@ void cstart() /* Record miscellaneous information for user-space servers. */ kinfo.nr_procs = NR_PROCS; kinfo.nr_tasks = NR_TASKS; - strncpy(kinfo.release, OS_RELEASE, sizeof(kinfo.release)); - kinfo.release[sizeof(kinfo.release)-1] = '\0'; - strncpy(kinfo.version, OS_VERSION, sizeof(kinfo.version)); - kinfo.version[sizeof(kinfo.version)-1] = '\0'; + strlcpy(kinfo.release, OS_RELEASE, sizeof(kinfo.release)); + strlcpy(kinfo.version, OS_VERSION, sizeof(kinfo.version)); /* Load average data initialization. */ kloadinfo.proc_last_slot = 0; diff --git a/kernel/proc.c b/kernel/proc.c index 0d5330693..98bab162e 100644 --- a/kernel/proc.c +++ b/kernel/proc.c @@ -30,11 +30,9 @@ */ #include -#include #include #include #include -#include #include #include "debug.h" @@ -47,6 +45,8 @@ #include "arch_proto.h" +#include + /* Scheduling and message passing functions */ static void idle(void); /** diff --git a/kernel/profile.c b/kernel/profile.c index 29e4ea3d4..55a2808cf 100644 --- a/kernel/profile.c +++ b/kernel/profile.c @@ -17,12 +17,13 @@ #include -#include -#include #include "kernel.h" #include "profile.h" #include "proc.h" +#include +#include + #if SPROFILE #include diff --git a/kernel/system/do_getinfo.c b/kernel/system/do_getinfo.c index 5ccc6abcb..bf87be9c0 100644 --- a/kernel/system/do_getinfo.c +++ b/kernel/system/do_getinfo.c @@ -10,7 +10,6 @@ */ #include -#include #include "kernel/system.h" diff --git a/kernel/system/do_safecopy.c b/kernel/system/do_safecopy.c index 86a433355..65f7230f5 100644 --- a/kernel/system/do_safecopy.c +++ b/kernel/system/do_safecopy.c @@ -14,10 +14,9 @@ */ #include -#include -#include #include "kernel/system.h" +#include "kernel.h" #define MAX_INDIRECT_DEPTH 5 /* up to how many indirect grants to follow? */ diff --git a/kernel/system/do_safemap.c b/kernel/system/do_safemap.c index 1b59b6e5f..12fa93dff 100644 --- a/kernel/system/do_safemap.c +++ b/kernel/system/do_safemap.c @@ -12,11 +12,10 @@ #include -#include -#include -#include - #include "kernel/system.h" +#include "kernel.h" + +#include #include @@ -119,9 +118,8 @@ int map_invoke_vm(struct proc * caller, endpoint_t end_s, vir_bytes off_s, size_t size, int flag) { - struct proc *src, *dst; + struct proc *dst; - src = endpoint_lookup(end_s); dst = endpoint_lookup(end_d); /* Make sure the linear addresses are both page aligned. */ diff --git a/lib/libsys/env_parse.c b/lib/libsys/env_parse.c index 8e93252cd..e466d2575 100644 --- a/lib/libsys/env_parse.c +++ b/lib/libsys/env_parse.c @@ -2,6 +2,7 @@ #include #include #include +#include /*=========================================================================* @@ -98,9 +99,9 @@ struct memory *mem_chunks; /* where to store the memory bits */ int maxchunks; /* how many were found */ { static kinfo_t kinfo; - int mm; + int mm, r; - sys_getkinfo(&kinfo); + if((r=sys_getkinfo(&kinfo)) != OK) return r; /* Initialize everything to zero. */ memset(mem_chunks, 0, maxchunks*sizeof(*mem_chunks)); diff --git a/servers/ds/store.h b/servers/ds/store.h index ccf7edc82..43e6ab12e 100644 --- a/servers/ds/store.h +++ b/servers/ds/store.h @@ -6,6 +6,7 @@ #include #include #include +#include #include #define NR_DS_KEYS (2*NR_SYS_PROCS) /* number of entries */ diff --git a/servers/is/glo.h b/servers/is/glo.h index ad5b06d70..fe9eed0c8 100644 --- a/servers/is/glo.h +++ b/servers/is/glo.h @@ -1,5 +1,7 @@ /* Global variables. */ +#include + /* Parameters needed to keep diagnostics at IS. */ #define DIAG_BUF_SIZE 1024 extern char diag_buf[DIAG_BUF_SIZE]; /* buffer for messages */ diff --git a/servers/pm/main.c b/servers/pm/main.c index 72750f4b8..2fe9fcd26 100644 --- a/servers/pm/main.c +++ b/servers/pm/main.c @@ -241,7 +241,7 @@ static int sef_cb_init_fresh(int UNUSED(type), sef_init_info_t *UNUSED(info)) /* Set process details found in the image table. */ rmp = &mproc[ip->proc_nr]; - strncpy(rmp->mp_name, ip->proc_name, PROC_NAME_LEN); + strlcpy(rmp->mp_name, ip->proc_name, PROC_NAME_LEN); (void) sigemptyset(&rmp->mp_ignore); (void) sigemptyset(&rmp->mp_sigmask); (void) sigemptyset(&rmp->mp_catch); diff --git a/servers/pm/pm.h b/servers/pm/pm.h index 5372bf773..1aa319f6c 100644 --- a/servers/pm/pm.h +++ b/servers/pm/pm.h @@ -16,6 +16,7 @@ #include #include #include +#include #include #include diff --git a/servers/procfs/glo.h b/servers/procfs/glo.h index c70890a67..03d573d71 100644 --- a/servers/procfs/glo.h +++ b/servers/procfs/glo.h @@ -1,6 +1,8 @@ #ifndef _PROCFS_GLO_H #define _PROCFS_GLO_H +#include + /* pid.c */ extern struct file pid_files[]; diff --git a/servers/rs/glo.h b/servers/rs/glo.h index d34a4b7eb..938e55f97 100644 --- a/servers/rs/glo.h +++ b/servers/rs/glo.h @@ -8,6 +8,8 @@ #define EXTERN #endif +#include + /* The boot image priv table. This table has entries for all system * services in the boot image. */ diff --git a/servers/vfs/glo.h b/servers/vfs/glo.h index dd1b6add1..8ad298abc 100644 --- a/servers/vfs/glo.h +++ b/servers/vfs/glo.h @@ -7,6 +7,8 @@ #define EXTERN #endif +#include + /* File System global variables */ EXTERN struct fproc *fp; /* pointer to caller's fproc struct */ EXTERN int susp_count; /* number of procs suspended on pipe */ diff --git a/servers/vm/arch/i386/pagetable.c b/servers/vm/arch/i386/pagetable.c index 1fc4e2fc8..00fc0e50a 100644 --- a/servers/vm/arch/i386/pagetable.c +++ b/servers/vm/arch/i386/pagetable.c @@ -522,7 +522,7 @@ int pt_map_in_range(struct vmproc *src_vmp, struct vmproc *dst_vmp, end = end ? end : VM_DATATOP; assert(start % I386_PAGE_SIZE == 0); assert(end % I386_PAGE_SIZE == 0); - assert(I386_VM_PDE(start) >= 0 && start <= end); + assert(start <= end); assert(I386_VM_PDE(end) < I386_VM_DIR_ENTRIES); #if LU_DEBUG diff --git a/servers/vm/glo.h b/servers/vm/glo.h index e0ba5366f..2d7998bdd 100644 --- a/servers/vm/glo.h +++ b/servers/vm/glo.h @@ -1,6 +1,7 @@ #include #include +#include #include #include #include diff --git a/servers/vm/main.c b/servers/vm/main.c index 4023ea4d6..d6692f3b1 100644 --- a/servers/vm/main.c +++ b/servers/vm/main.c @@ -276,7 +276,7 @@ void exec_bootproc(struct vmproc *vmp, struct boot_image *ip) execi->proc_e = vmp->vm_endpoint; execi->hdr = hdr; execi->hdr_len = sizeof(hdr); - strcpy(execi->progname, ip->proc_name); + strlcpy(execi->progname, ip->proc_name, sizeof(execi->progname)); execi->frame_len = 0; execi->opaque = &vmexeci; @@ -370,9 +370,10 @@ void init_vm(void) } /* Set up table of calls. */ -#define CALLMAP(code, func) { int i; \ - if((i=CALLNUMBER(code)) < 0) { panic(#code " invalid: %d", (code)); } \ - if(i >= NR_VM_CALLS) { panic(#code " invalid: %d", (code)); } \ +#define CALLMAP(code, func) { int i; \ + i=CALLNUMBER(code); \ + assert(i >= 0); \ + assert(i < NR_VM_CALLS); \ vm_calls[i].vmc_func = (func); \ vm_calls[i].vmc_name = #code; \ }