From 428b449a6ca076c545fc539979e67903819058d4 Mon Sep 17 00:00:00 2001 From: Paul Floyd Date: Sat, 9 Sep 2023 08:53:43 +0200 Subject: [PATCH] coverity: various minor fixes mostly asserts that unsigned values are 0 or positive --- coregrind/m_machine.c | 8 ++++---- coregrind/m_mallocfree.c | 4 ++-- coregrind/m_syswrap/syswrap-generic.c | 1 + coregrind/m_syswrap/syswrap-main.c | 8 ++++---- coregrind/m_transtab.c | 18 +++++++++--------- coregrind/vgdb.c | 2 +- helgrind/hg_main.c | 2 +- include/vki/vki-freebsd.h | 2 -- memcheck/mc_main.c | 7 +++---- 9 files changed, 25 insertions(+), 27 deletions(-) diff --git a/coregrind/m_machine.c b/coregrind/m_machine.c index 052b5d186..661a3f107 100644 --- a/coregrind/m_machine.c +++ b/coregrind/m_machine.c @@ -414,28 +414,28 @@ Bool VG_(thread_stack_next)(/*MOD*/ThreadId* tid, Addr VG_(thread_get_stack_max)(ThreadId tid) { - vg_assert(0 <= tid && tid < VG_N_THREADS && tid != VG_INVALID_THREADID); + vg_assert(tid < VG_N_THREADS && tid != VG_INVALID_THREADID); vg_assert(VG_(threads)[tid].status != VgTs_Empty); return VG_(threads)[tid].client_stack_highest_byte; } SizeT VG_(thread_get_stack_size)(ThreadId tid) { - vg_assert(0 <= tid && tid < VG_N_THREADS && tid != VG_INVALID_THREADID); + vg_assert(tid < VG_N_THREADS && tid != VG_INVALID_THREADID); vg_assert(VG_(threads)[tid].status != VgTs_Empty); return VG_(threads)[tid].client_stack_szB; } Addr VG_(thread_get_altstack_min)(ThreadId tid) { - vg_assert(0 <= tid && tid < VG_N_THREADS && tid != VG_INVALID_THREADID); + vg_assert(tid < VG_N_THREADS && tid != VG_INVALID_THREADID); vg_assert(VG_(threads)[tid].status != VgTs_Empty); return (Addr)VG_(threads)[tid].altstack.ss_sp; } SizeT VG_(thread_get_altstack_size)(ThreadId tid) { - vg_assert(0 <= tid && tid < VG_N_THREADS && tid != VG_INVALID_THREADID); + vg_assert(tid < VG_N_THREADS && tid != VG_INVALID_THREADID); vg_assert(VG_(threads)[tid].status != VgTs_Empty); return VG_(threads)[tid].altstack.ss_size; } diff --git a/coregrind/m_mallocfree.c b/coregrind/m_mallocfree.c index b58d471b6..44beb3d8b 100644 --- a/coregrind/m_mallocfree.c +++ b/coregrind/m_mallocfree.c @@ -918,11 +918,11 @@ void reclaimSuperblock ( Arena* a, Superblock* sb) cszB = sizeof(Superblock) + sb->n_payload_bytes; // removes sb from superblock list. - for (i = 0; i < a->sblocks_used; i++) { + for (i = 0U; i < a->sblocks_used; i++) { if (a->sblocks[i] == sb) break; } - vg_assert(i >= 0 && i < a->sblocks_used); + vg_assert(i < a->sblocks_used); for (j = i; j < a->sblocks_used; j++) a->sblocks[j] = a->sblocks[j+1]; a->sblocks_used--; diff --git a/coregrind/m_syswrap/syswrap-generic.c b/coregrind/m_syswrap/syswrap-generic.c index ed9d14685..d2ff8c0f4 100644 --- a/coregrind/m_syswrap/syswrap-generic.c +++ b/coregrind/m_syswrap/syswrap-generic.c @@ -1830,6 +1830,7 @@ UInt get_sem_count( Int semid ) if (sr_isError(res)) return 0; + // both clang-tidy and coverity complain about this but I think they are both wrong return buf.sem_nsems; # elif defined(__NR_semsys) /* Solaris */ struct vki_semid_ds buf; diff --git a/coregrind/m_syswrap/syswrap-main.c b/coregrind/m_syswrap/syswrap-main.c index 4f8c0fe1c..91a1f7e53 100644 --- a/coregrind/m_syswrap/syswrap-main.c +++ b/coregrind/m_syswrap/syswrap-main.c @@ -1984,26 +1984,26 @@ SyscallInfo *syscallInfo; void VG_(clear_syscallInfo) ( ThreadId tid ) { vg_assert(syscallInfo); - vg_assert(tid >= 0 && tid < VG_N_THREADS); + vg_assert(tid < VG_N_THREADS); VG_(memset)( & syscallInfo[tid], 0, sizeof( syscallInfo[tid] )); syscallInfo[tid].status.what = SsIdle; } Bool VG_(is_in_syscall) ( ThreadId tid ) { - vg_assert(tid >= 0 && tid < VG_N_THREADS); + vg_assert(tid < VG_N_THREADS); return (syscallInfo && syscallInfo[tid].status.what != SsIdle); } Bool VG_(is_in_kernel_restart_syscall) ( ThreadId tid ) { - vg_assert(tid >= 0 && tid < VG_N_THREADS); + vg_assert(tid < VG_N_THREADS); return (syscallInfo && ((syscallInfo[tid].flags & SfKernelRestart) != 0)); } Word VG_(is_in_syscall_no) (ThreadId tid ) { - vg_assert(tid >= 0 && tid < VG_N_THREADS); + vg_assert(tid < VG_N_THREADS); return syscallInfo[tid].orig_args.sysno; } diff --git a/coregrind/m_transtab.c b/coregrind/m_transtab.c index 102108a35..5e82d57d9 100644 --- a/coregrind/m_transtab.c +++ b/coregrind/m_transtab.c @@ -92,10 +92,10 @@ typedef UShort HTTno; address range which does not fall cleanly within any specific bin. Note that ECLASS_SHIFT + ECLASS_WIDTH must be < 32. ECLASS_N must fit in a EclassNo. */ -#define ECLASS_SHIFT 13 -#define ECLASS_WIDTH 9 -#define ECLASS_MISC (1 << ECLASS_WIDTH) -#define ECLASS_N (1 + ECLASS_MISC) +#define ECLASS_SHIFT 13U +#define ECLASS_WIDTH 9U +#define ECLASS_MISC (1U << ECLASS_WIDTH) +#define ECLASS_N (1U + ECLASS_MISC) STATIC_ASSERT(ECLASS_SHIFT + ECLASS_WIDTH < 32); typedef UShort EClassNo; @@ -1625,11 +1625,11 @@ static void initialiseSector ( SECno sno ) sizeof(HostExtent)); /* Add an entry in the sector_search_order */ - for (i = 0; i < n_sectors; i++) { + for (i = 0U; i < n_sectors; i++) { if (sector_search_order[i] == INV_SNO) break; } - vg_assert(i >= 0 && i < n_sectors); + vg_assert(i < n_sectors); sector_search_order[i] = sno; if (VG_(clo_verbosity) > 2) @@ -1984,7 +1984,7 @@ Bool VG_(search_transtab) ( /*OUT*/Addr* res_hcode, /*-------------------------------------------------------------*/ /* forward */ -static void unredir_discard_translations( Addr, ULong ); +static void unredir_discard_translations( Addr /*guest_start*/, ULong /*range*/); /* Stuff for deleting translations which intersect with a given address range. Unfortunately, to make this run at a reasonable @@ -2237,7 +2237,7 @@ void VG_(discard_translations) ( Addr guest_start, ULong range, " FAST, ec = %d\n", ec); /* Fast scheme */ - vg_assert(ec >= 0 && ec < ECLASS_MISC); + vg_assert(ec < ECLASS_MISC); for (sno = 0; sno < n_sectors; sno++) { sec = §ors[sno]; @@ -2343,7 +2343,7 @@ void VG_(discard_translations_safely) ( Addr start, SizeT len, #define UNREDIR_SZB 1000 #define N_UNREDIR_TT 500 -#define N_UNREDIR_TCQ (N_UNREDIR_TT * UNREDIR_SZB / sizeof(ULong)) +#define N_UNREDIR_TCQ (N_UNREDIR_TT * UNREDIR_SZB / (Int)sizeof(ULong)) typedef struct { diff --git a/coregrind/vgdb.c b/coregrind/vgdb.c index 8e030e27b..872c7d280 100644 --- a/coregrind/vgdb.c +++ b/coregrind/vgdb.c @@ -1039,7 +1039,7 @@ static int receive_packet(char *buf, int noackmode) int ret; char c; char c1 = '\0'; - char c2; + char c2 = '\0'; unsigned char csum = 0; // Look for first '$' (start of packet) or error. diff --git a/helgrind/hg_main.c b/helgrind/hg_main.c index b193d07d6..45e6388b6 100644 --- a/helgrind/hg_main.c +++ b/helgrind/hg_main.c @@ -3328,7 +3328,7 @@ static void evh__HG_PTHREAD_BARRIER_RESIZE_PRE ( ThreadId tid, the barrier, so need to mess with dep edges in the same way as if the barrier had filled up normally. */ present = VG_(sizeXA)(bar->waiting); - tl_assert(present >= 0 && present <= bar->size); + tl_assert(present <= bar->size); if (newcount <= present) { bar->size = present; /* keep the cross_sync call happy */ do_barrier_cross_sync_and_empty(bar); diff --git a/include/vki/vki-freebsd.h b/include/vki/vki-freebsd.h index eee094d34..a7fed6433 100644 --- a/include/vki/vki-freebsd.h +++ b/include/vki/vki-freebsd.h @@ -1321,8 +1321,6 @@ struct vki_semid_ds { unsigned short sem_nsems; /* no. of semaphores in array */ vki_time_t sem_otime; /* last semop time */ vki_time_t sem_ctime; /* last change time */ - long sem_pad2; - long sem_pad3[4]; }; struct vki_sembuf { diff --git a/memcheck/mc_main.c b/memcheck/mc_main.c index 3f34e3dc1..e86487a57 100644 --- a/memcheck/mc_main.c +++ b/memcheck/mc_main.c @@ -2984,8 +2984,7 @@ void make_aligned_word64_noaccess ( Addr a ) if (UNLIKELY( MC_(clo_mc_level) == 3 )) { OCacheLine* line; UWord lineoff = oc_line_offset(a); - tl_assert(lineoff >= 0 - && lineoff < OC_W32S_PER_LINE -1/*'cos 8-aligned*/); + tl_assert(lineoff < OC_W32S_PER_LINE -1/*'cos 8-aligned*/); line = find_OCacheLine( a ); line->u.main.descr[lineoff+0] = 0; line->u.main.descr[lineoff+1] = 0; @@ -6048,8 +6047,8 @@ static Bool mc_expensive_sanity_check ( void ) --partial-loads-ok needs to be enabled by default on all platforms. Not doing so causes lots of false errors. */ Bool MC_(clo_partial_loads_ok) = True; -Long MC_(clo_freelist_vol) = 20*1000*1000LL; -Long MC_(clo_freelist_big_blocks) = 1*1000*1000LL; +Long MC_(clo_freelist_vol) = 20LL*1000LL*1000LL; +Long MC_(clo_freelist_big_blocks) = 1LL*1000LL*1000LL; LeakCheckMode MC_(clo_leak_check) = LC_Summary; VgRes MC_(clo_leak_resolution) = Vg_HighRes; UInt MC_(clo_show_leak_kinds) = R2S(Possible) | R2S(Unreached); -- 2.11.4.GIT