Always check the length before returning the head or tail

This commit is contained in:
wargio 2024-04-23 10:33:49 +08:00 committed by Giovanni
parent 34f1a9e7b4
commit 80cd0e9ae5

View file

@ -292,12 +292,18 @@ static inline void **rz_pvector_data(RzPVector *vec) {
// returns the first element of the vector // returns the first element of the vector
static inline void *rz_pvector_head(RzPVector *vec) { static inline void *rz_pvector_head(RzPVector *vec) {
rz_return_val_if_fail(vec, NULL); rz_return_val_if_fail(vec, NULL);
if (vec->v.len < 1) {
return NULL;
}
return ((void **)vec->v.a)[0]; return ((void **)vec->v.a)[0];
} }
// returns the last element of the vector // returns the last element of the vector
static inline void *rz_pvector_tail(RzPVector *vec) { static inline void *rz_pvector_tail(RzPVector *vec) {
rz_return_val_if_fail(vec, NULL); rz_return_val_if_fail(vec, NULL);
if (vec->v.len < 1) {
return NULL;
}
return ((void **)vec->v.a)[vec->v.len - 1]; return ((void **)vec->v.a)[vec->v.len - 1];
} }