Check for empty vector to prevent OOB reads and writes. (#5591)

* Check for empty vector to prevent OOB reads and writes.

* Add tests to check for OOB reads/writes.
This commit is contained in:
Rot127
2026-02-10 16:57:12 +00:00
committed by GitHub
parent 38407306b3
commit 6a8d2ecf57
2 changed files with 19 additions and 6 deletions
+9 -3
View File
@@ -185,7 +185,9 @@ RZ_API void *rz_vector_assign_at(RzVector *vec, size_t index, void *elem) {
}
RZ_API void rz_vector_remove_at(RzVector *vec, size_t index, void *into) {
rz_return_if_fail(vec);
if (rz_vector_empty(vec)) {
return;
}
void *p = rz_vector_index_ptr(vec, index);
if (into) {
rz_vector_assign(vec, into, p);
@@ -254,7 +256,9 @@ RZ_API void *rz_vector_insert_range(RzVector *vec, size_t index, RZ_NULLABLE voi
}
RZ_API void rz_vector_pop(RzVector *vec, void *into) {
rz_return_if_fail(vec);
if (rz_vector_empty(vec)) {
return;
}
if (into) {
rz_vector_assign(vec, into, rz_vector_index_ptr(vec, vec->len - 1));
}
@@ -262,7 +266,9 @@ RZ_API void rz_vector_pop(RzVector *vec, void *into) {
}
RZ_API void rz_vector_pop_front(RzVector *vec, void *into) {
rz_return_if_fail(vec);
if (rz_vector_empty(vec)) {
return;
}
rz_vector_remove_at(vec, 0, into);
}
+10 -3
View File
@@ -278,7 +278,10 @@ static bool test_vector_empty(void) {
}
static bool test_vector_remove_at(void) {
RzVector v;
RzVector v = { 0 };
// Check it doesn't read/writes OOB.
rz_vector_remove_at(&v, 0, NULL);
init_test_vector(&v, 5, 0, NULL, NULL);
ut32 e;
@@ -471,7 +474,9 @@ static bool test_vector_insert_range(void) {
}
static bool test_vector_pop(void) {
RzVector v;
RzVector v = { 0 };
// Check it doesn't read/writes OOB.
rz_vector_pop(&v, NULL);
init_test_vector(&v, 3, 0, NULL, NULL);
ut32 e;
@@ -496,7 +501,9 @@ static bool test_vector_pop(void) {
}
static bool test_vector_pop_front(void) {
RzVector v;
RzVector v = { 0 };
// Check it doesn't read/writes OOB.
rz_vector_pop_front(&v, NULL);
init_test_vector(&v, 3, 0, NULL, NULL);
ut32 e;