Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Coverity defects #2469

Merged
merged 56 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
d03d05d
Fix CID 508418 - Uninitialized struct
Rot127 Sep 3, 2024
b61d969
Fix CID 509089 - Fix OOB read and write
Rot127 Sep 3, 2024
59d5902
Fix CID 509088 - OOB.
Rot127 Sep 4, 2024
774204e
Fix CID 509085 - Resource leak.
Rot127 Sep 4, 2024
fe5448f
Fix CID 508414 and companions - Using undefined values.
Rot127 Sep 4, 2024
a9a2c79
Fix CID 508405 - Use of uninitialized value
Rot127 Sep 4, 2024
1e55a6a
Remove unnecessary and badly implemented dev fuzz code.
Rot127 Sep 4, 2024
cde7ad7
Fix CID 508396 - Uninitialzied variable.
Rot127 Sep 4, 2024
7a3c276
Fix CID 508393, 508365 -- OOB read.
Rot127 Sep 4, 2024
9db4f22
Fix CID 432207 - OVerlapping memory access.
Rot127 Sep 4, 2024
75cb36d
Remove unused functions
Rot127 Sep 4, 2024
12e49d5
Fix CID 432170 - Overlapping memory access.
Rot127 Sep 4, 2024
baa82a9
Fix CID 166022 - Check for negative index
Rot127 Sep 4, 2024
d54ffb6
Let strncat not depend n src operand.
Rot127 Sep 5, 2024
e0fe7ed
Fix 509083 and 509084 - NULL dereference
Rot127 Sep 5, 2024
6e19b46
Remove duplicated code.
Rot127 Sep 5, 2024
15822e7
Initialize sysop
Rot127 Sep 5, 2024
346b663
Fix resource leak
Rot127 Sep 5, 2024
85e4ee6
Remove unreachable code.
Rot127 Sep 5, 2024
f428c8e
Remove duplicate code.
Rot127 Sep 5, 2024
84ea220
Add assert to check return value of cmoack
Rot127 Sep 5, 2024
d0c63ab
Fixed: d should be a signed value, since it is checked against < 0
Rot127 Sep 5, 2024
946f386
Add missing break.
Rot127 Sep 5, 2024
eed1bf5
Add NULL check
Rot127 Sep 5, 2024
84c5642
Fix signs of binary search comparisons.
Rot127 Sep 5, 2024
56f1e9c
Add explicit cast of or result
Rot127 Sep 5, 2024
f8ee8c4
Fix correct scope of case.
Rot127 Sep 5, 2024
be241a8
Handle invalid integer type.
Rot127 Sep 5, 2024
4999134
Return UINT_MAX instead of implicitly casted -1
Rot127 Sep 5, 2024
ecbea15
Remove dead code
Rot127 Sep 5, 2024
dfbedd1
Fix type of im
Rot127 Sep 5, 2024
56ae354
Fix type of d
Rot127 Sep 5, 2024
a35f992
Remove duplicated code.
Rot127 Sep 5, 2024
1e83cc2
Add returns after CS_ASSERTS
Rot127 Sep 5, 2024
a52ad7b
Check for len == 0 case.
Rot127 Sep 5, 2024
891eadd
Ensure shift operates on uint64
Rot127 Sep 5, 2024
6670d4e
Replace strcpy with strncpy.
Rot127 Sep 5, 2024
d47097e
Handle edge cases for 32bit rotate
Rot127 Sep 5, 2024
2cf22f5
Fix some out of enum warnings
Rot127 Sep 5, 2024
9809f8d
Replace a strcpy with strncpy.
Rot127 Sep 5, 2024
68ed50f
Fix increment of address
Rot127 Sep 5, 2024
6a1b96e
Skip some linting
Rot127 Sep 5, 2024
eb353e1
Fix: set instruction id
Rot127 Sep 5, 2024
5f5cdc5
Remove unused enum
Rot127 Sep 6, 2024
94bbb1f
Replace the last usages of strcpy with SStream functions.
Rot127 Sep 6, 2024
77ebd43
Increase number of allowed AArch64 operands.
Rot127 Sep 6, 2024
e2ea951
Check safety of incrementing t the next operand.
Rot127 Sep 6, 2024
2a854c3
Fix naming of operand
Rot127 Sep 6, 2024
4e7463a
Update python constants
Rot127 Sep 6, 2024
1b613fe
Fix option setup of CS_OPT_DETAIL_REAL
Rot127 Sep 6, 2024
16cb257
Document DETAIL_REAL has to be used with CS_OPT_ON.
Rot127 Sep 6, 2024
7a44c8b
Run Coverity scan every Monday.
Rot127 Sep 6, 2024
b2ab5e0
Remove dead code
Rot127 Sep 7, 2024
f8ae3b6
Fix OOB read
Rot127 Sep 7, 2024
e99998b
Rename macro to reflect it is only used with sstreams
Rot127 Sep 7, 2024
9523b8d
Fix rebase issues
Rot127 Sep 10, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/coverity.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Coverity Scan
on:
workflow_dispatch:
schedule:
- cron: '0 0 01 * *' # On the 1st every month at midnight UTC
- cron: '0 0 * * 1' # On every Monday at midnight UTC


# Automatically cancel any previous workflow on new push.
Expand Down
4 changes: 2 additions & 2 deletions MCInstPrinter.c
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,8 @@ unsigned int binsearch_IndexTypeStrEncoding(const struct IndexTypeStr *index, si

right = size - 1;

size_t str_left_cmp = strcmp(name, index[0].name);
size_t str_right_cmp = strcmp(name, index[right].name);
int str_left_cmp = strcmp(name, index[0].name);
int str_right_cmp = strcmp(name, index[right].name);
if (str_left_cmp < 0 || str_right_cmp > 0)
// not found
return -1;
Expand Down
12 changes: 9 additions & 3 deletions Mapping.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,10 @@ void map_implicit_reads(MCInst *MI, const insn_map *imap)
return;
}
detail->regs_read[detail->regs_read_count++] = reg;
reg = imap[Opcode].regs_use[++i];
if (i + 1 < MAX_IMPL_R_REGS) {
// Select next one
reg = imap[Opcode].regs_use[++i];
}
}
#endif // CAPSTONE_DIET
}
Expand All @@ -175,7 +178,10 @@ void map_implicit_writes(MCInst *MI, const insn_map *imap)
return;
}
detail->regs_write[detail->regs_write_count++] = reg;
reg = imap[Opcode].regs_mod[++i];
if (i + 1 < MAX_IMPL_W_REGS) {
// Select next one
reg = imap[Opcode].regs_mod[++i];
}
}
#endif // CAPSTONE_DIET
}
Expand Down Expand Up @@ -348,7 +354,7 @@ DEFINE_get_detail_op(systemz, SystemZ);
/// So it can be toggled between disas() calls.
bool map_use_alias_details(const MCInst *MI) {
assert(MI);
return !(MI->csh->detail_opt & CS_OPT_DETAIL_REAL);
return (MI->csh->detail_opt & CS_OPT_ON) && !(MI->csh->detail_opt & CS_OPT_DETAIL_REAL);
}

/// Sets the setDetailOps flag to @p Val.
Expand Down
16 changes: 16 additions & 0 deletions Mapping.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,22 @@ DEFINE_get_arch_detail(mips, Mips);
DEFINE_get_arch_detail(riscv, RISCV);
DEFINE_get_arch_detail(systemz, SystemZ);

#define DEFINE_check_safe_inc(Arch, ARCH) \
static inline void Arch##_check_safe_inc() { \
CS_ASSERT(Arch##_get_detail(MI)->op_count + 1 < NUM_##ARCH##_OPS); \
}

DEFINE_check_safe_inc(ARM, ARM);
DEFINE_check_safe_inc(PPC, PPC);
DEFINE_check_safe_inc(TriCore, TRICORE);
DEFINE_check_safe_inc(AArch64, AARCH64);
DEFINE_check_safe_inc(Alpha, ALPHA);
DEFINE_check_safe_inc(HPPA, HPPA);
DEFINE_check_safe_inc(LoongArch, LOONGARCH);
DEFINE_check_safe_inc(RISCV, RISCV);
DEFINE_check_safe_inc(SystemZ, SYSTEMZ);
DEFINE_check_safe_inc(Mips, MIPS);

static inline bool detail_is_set(const MCInst *MI)
{
assert(MI && MI->flat_insn);
Expand Down
12 changes: 10 additions & 2 deletions MathExtras.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#endif
#endif

#include <limits.h>

// NOTE: The following support functions use the _32/_64 extensions instead of
// type overloading so that signed and unsigned integers can be used without
// ambiguity.
Expand Down Expand Up @@ -280,15 +282,21 @@ static inline unsigned CountPopulation_64(uint64_t Value) {
}

/// Log2_32 - This function returns the floor log base 2 of the specified value,
/// -1 if the value is zero. (32 bit edition.)
/// UINT_MAX if the value is zero. (32 bit edition.)
/// Ex. Log2_32(32) == 5, Log2_32(1) == 0, Log2_32(0) == -1, Log2_32(6) == 2
static inline unsigned Log2_32(uint32_t Value) {
if (Value == 0) {
return UINT_MAX;
}
return 31 - CountLeadingZeros_32(Value);
}

/// Log2_64 - This function returns the floor log base 2 of the specified value,
/// -1 if the value is zero. (64 bit edition.)
/// UINT_MAX if the value is zero. (64 bit edition.)
static inline unsigned Log2_64(uint64_t Value) {
if (Value == 0) {
return UINT32_MAX;
}
return 63 - CountLeadingZeros_64(Value);
}

Expand Down
98 changes: 93 additions & 5 deletions SStream.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,108 @@
#include "cs_priv.h"
#include "utils.h"

#ifdef _MSC_VER
#pragma warning(disable: 4996) // disable MSVC's warning on strcpy()
#endif

void SStream_Init(SStream *ss)
{
assert(ss);
ss->index = 0;
ss->buffer[0] = '\0';
memset(ss->buffer, 0, sizeof(ss->buffer));
ss->is_closed = false;
ss->markup_stream = false;
ss->prefixed_by_markup = false;
}

/// Returns the a pointer to the internal string buffer of the stream.
/// For reading only.
const char *SStream_rbuf(const SStream *ss) {
assert(ss);
return ss->buffer;
}

/// Searches in the stream for the first (from the left) occurrence of @elem and replaces
/// it with @repl. It returns the pointer *after* the replaced character
/// or NULL if no character was replaced.
///
/// It will never replace the final \0 byte in the stream buffer.
const char *SStream_replc(const SStream *ss, char elem, char repl) {
assert(ss);
char *found = strchr(ss->buffer, elem);
if (!found || found == ss->buffer + (SSTREAM_BUF_LEN - 1)) {
return NULL;
}
*found = repl;
found++;
return found;
}

/// Searches in the stream for the first (from the left) occurrence of @chr and replaces
/// it with @rstr.
void SStream_replc_str(SStream *ss, char chr, const char *rstr) {
assert(ss && rstr);
char *found = strchr(ss->buffer, chr);
if (!found || found == ss->buffer + (SSTREAM_BUF_LEN - 1)) {
return;
}
size_t post_len = strlen(found + 1);
size_t buf_str_len = strlen(ss->buffer);
size_t repl_len = strlen(rstr);
if (repl_len - 1 + buf_str_len >= SSTREAM_BUF_LEN) {
return;
}
memmove(found + repl_len, found + 1, post_len);
memcpy(found, rstr, repl_len);
ss->index = strlen(ss->buffer);
}

/// Removes the space characters '\t' and ' ' from the beginning of the stream buffer.
void SStream_trimls(SStream *ss) {
assert(ss);
size_t buf_off = 0;
/// Remove leading spaces
while (ss->buffer[buf_off] == ' ' || ss->buffer[buf_off] == '\t') {
buf_off++;
}
if (buf_off > 0) {
memmove(ss->buffer, ss->buffer + buf_off, SSTREAM_BUF_LEN - buf_off);
ss->index -= buf_off;
}
}

/// Extract the mnemonic to @mnem_buf and the operand string into @op_str_buf from the stream buffer.
/// The mnemonic is everything up until the first ' ' or '\t' character.
/// The operand string is everything after the first ' ' or '\t' sequence.
void SStream_extract_mnem_opstr(const SStream *ss, char *mnem_buf, size_t mnem_buf_size, char *op_str_buf, size_t op_str_buf_size) {
assert(ss && mnem_buf && mnem_buf_size > 0 && op_str_buf && op_str_buf_size > 0);
size_t off = 0;
// Copy all non space chars to as mnemonic.
while (ss->buffer[off] && ss->buffer[off] != ' ' && ss->buffer[off] != '\t') {
if (off < mnem_buf_size - 1) {
// Only copy if there is space left.
mnem_buf[off] = ss->buffer[off];
}
off++;
}
if (!ss->buffer[off]) {
return;
}

// Iterate until next non space char.
do {
off++;
} while (ss->buffer[off] && (ss->buffer[off] == ' ' || ss->buffer[off] == '\t'));

if (!ss->buffer[off]) {
return;
}

// Copy all follow up characters as op_str
const char *ss_op_str = ss->buffer + off;
off = 0;
while (ss_op_str[off] && off < op_str_buf_size - 1) {
op_str_buf[off] = ss_op_str[off];
off++;
}
}

/// Empty the stream @ss to given @file (stdin/stderr).
/// @file can be NULL. Then the buffer content is not emitted.
void SStream_Flush(SStream *ss, FILE *file)
Expand Down
10 changes: 10 additions & 0 deletions SStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ do { \

void SStream_Init(SStream *ss);

const char *SStream_replc(const SStream *ss, char elem, char repl);

void SStream_replc_str(SStream *ss, char chr, const char *rstr);

const char *SStream_rbuf(const SStream *ss);

void SStream_extract_mnem_opstr(const SStream *ss, char *mnem_buf, size_t mnem_buf_size, char *op_str_buf, size_t op_str_buf_size);

void SStream_trimls(SStream *ss);

void SStream_Flush(SStream *ss, FILE *file);

void SStream_Open(SStream *ss);
Expand Down
5 changes: 4 additions & 1 deletion arch/AArch64/AArch64AddressingModes.h
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,10 @@ static inline uint64_t AArch64_AM_decodeLogicalImmediate(uint64_t val,
unsigned imms = val & 0x3f;

int len = 31 - countLeadingZeros((N << 6) | (~imms & 0x3f));
assert(len >= 1);
if (len < 1) {
assert(len >= 1 && "Unhandled integer type");
return 0;
}

unsigned size = (1 << len);
unsigned R = immr & (size - 1);
Expand Down
10 changes: 5 additions & 5 deletions arch/AArch64/AArch64InstPrinter.c
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,7 @@ Search_IC: {
!AArch64_testFeatureList(MI->csh->mode, IC->FeaturesRequired))
return false;
if (detail_is_set(MI)) {
aarch64_sysop sysop;
aarch64_sysop sysop = { 0 };
sysop.reg = IC->SysReg;
sysop.sub_type = AARCH64_OP_IC;
AArch64_get_detail_op(MI, 0)->type = AARCH64_OP_SYSREG;
Expand All @@ -754,7 +754,7 @@ Search_IC: {
MI->csh->mode, DC->FeaturesRequired))
return false;
if (detail_is_set(MI)) {
aarch64_sysop sysop;
aarch64_sysop sysop = { 0 };
sysop.alias = DC->SysAlias;
sysop.sub_type = AARCH64_OP_DC;
AArch64_get_detail_op(MI, 0)->type =
Expand All @@ -777,7 +777,7 @@ Search_IC: {
return false;

if (detail_is_set(MI)) {
aarch64_sysop sysop;
aarch64_sysop sysop = { 0 };
sysop.alias = AT->SysAlias;
sysop.sub_type = AARCH64_OP_AT;
AArch64_get_detail_op(MI, 0)->type =
Expand All @@ -799,7 +799,7 @@ Search_IC: {
return false;

if (detail_is_set(MI)) {
aarch64_sysop sysop;
aarch64_sysop sysop = { 0 };
sysop.reg = TLBI->SysReg;
sysop.sub_type = AARCH64_OP_TLBI;
AArch64_get_detail_op(MI, 0)->type = AARCH64_OP_SYSREG;
Expand Down Expand Up @@ -868,7 +868,7 @@ bool printSyspAlias(MCInst *MI, SStream *O)
return false;

if (detail_is_set(MI)) {
aarch64_sysop sysop;
aarch64_sysop sysop = { 0 };
sysop.reg = TLBI->SysReg;
sysop.sub_type = AARCH64_OP_TLBI;
AArch64_get_detail_op(MI, 0)->type = AARCH64_OP_SYSREG;
Expand Down
Loading
Loading