Skip to content

Commit 623935d

Browse files
committed
target/riscv: fix potential UB reported by ubsan in ac cache lookup
when running OpenOCD built with ubsan enabled I've encountered the following error message: ``` riscv-013.c:204:9: runtime error: null pointer passed as argument 2, ... ``` This is caused by a NULL pointer passed to bsearch function when abstract command cache is still empty. This behavor was introduced in commit ab97974 ("target/riscv: implement abstract command cache") Signed-off-by: Parshintsev Anatoly <[email protected]>
1 parent 608ba43 commit 623935d

File tree

1 file changed

+2
-0
lines changed

1 file changed

+2
-0
lines changed

src/target/riscv/riscv-013.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,8 @@ static void ac_cache_insert(struct ac_cache *cache, uint32_t command)
201201

202202
static bool ac_cache_contains(const struct ac_cache *cache, uint32_t command)
203203
{
204+
if (cache->size == 0)
205+
return false;
204206
return bsearch(&command, cache->commands, cache->size,
205207
sizeof(*cache->commands), ac_cache_elem_comparator);
206208
}

0 commit comments

Comments
 (0)