From bbcf3808698e47c7e4fa349f11420a9ad101eb8f Mon Sep 17 00:00:00 2001 From: erickson Date: Mon, 7 May 2007 14:26:56 +0000 Subject: [PATCH] added some more index sanity checks. added const qualifier where necessary git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@875 9efc2488-bf62-4759-914b-345cdb29e865 --- src/libstack/osrf_list.c | 16 +++++++--------- src/libstack/osrf_list.h | 12 ++++++------ 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/src/libstack/osrf_list.c b/src/libstack/osrf_list.c index 0054c79..0e58f0f 100644 --- a/src/libstack/osrf_list.c +++ b/src/libstack/osrf_list.c @@ -54,14 +54,13 @@ void* osrfListSet( osrfList* list, void* item, unsigned int position ) { void* olditem = osrfListRemove( list, position ); list->arrlist[position] = item; - //if( list->size == 0 || list->size <= position ) if( list->size <= position ) list->size = position + 1; return olditem; } -void* osrfListGetIndex( osrfList* list, unsigned int position ) { - if(!list || position >= list->size) return NULL; +void* osrfListGetIndex( const osrfList* list, unsigned int position ) { + if(!list || position >= list->size || position < 0) return NULL; return list->arrlist[position]; } @@ -80,8 +79,8 @@ void osrfListFree( osrfList* list ) { free(list); } -void* osrfListRemove( osrfList* list, int position ) { - if(!list || position >= list->size) return NULL; +void* osrfListRemove( osrfList* list, unsigned int position ) { + if(!list || position >= list->size || position < 0) return NULL; void* olditem = list->arrlist[position]; list->arrlist[position] = NULL; @@ -95,7 +94,7 @@ void* osrfListRemove( osrfList* list, int position ) { } -int osrfListFind( osrfList* list, void* addr ) { +int osrfListFind( const osrfList* list, void* addr ) { if(!(list && addr)) return -1; int index; for( index = 0; index < list->size; index++ ) { @@ -106,7 +105,7 @@ int osrfListFind( osrfList* list, void* addr ) { } -unsigned int osrfListGetCount( osrfList* list ) { +unsigned int osrfListGetCount( const osrfList* list ) { if(!list) return -1; return list->size; } @@ -118,9 +117,8 @@ void* osrfListPop( osrfList* list ) { } -osrfListIterator* osrfNewListIterator( osrfList* list ) { +osrfListIterator* osrfNewListIterator( const osrfList* list ) { if(!list) return NULL; - //osrfListIterator* itr = safe_malloc(sizeof(osrfListIterator)); osrfListIterator* itr; OSRF_MALLOC(itr, sizeof(osrfListIterator)); itr->list = list; diff --git a/src/libstack/osrf_list.h b/src/libstack/osrf_list.h index cc1a2b7..d829ab7 100644 --- a/src/libstack/osrf_list.h +++ b/src/libstack/osrf_list.h @@ -26,7 +26,7 @@ typedef struct __osrfListStruct osrfList; struct __osrfListIteratorStruct { - osrfList* list; + const osrfList* list; unsigned int current; }; typedef struct __osrfListIteratorStruct osrfListIterator; @@ -37,7 +37,7 @@ osrfList* osrfNewListSize( unsigned int size ); /** Creates a new list iterator with the given list */ -osrfListIterator* osrfNewListIterator( osrfList* list ); +osrfListIterator* osrfNewListIterator( const osrfList* list ); /** Returns the next non-NULL item in the list, return NULL when @@ -99,7 +99,7 @@ void* osrfListSet( osrfList* list, void* item, unsigned int position ); @param list The list @param postiont the position */ -void* osrfListGetIndex( osrfList* list, unsigned int position ); +void* osrfListGetIndex( const osrfList* list, unsigned int position ); /** Frees the list and all list items (if the list has a "freeItem" function defined ) @@ -114,7 +114,7 @@ void osrfListFree( osrfList* list ); @return A pointer to the item removed if "freeItem" is not defined for this list, returns NULL if it is. */ -void* osrfListRemove( osrfList* list, int position ); +void* osrfListRemove( osrfList* list, unsigned int position ); /** Finds the list item whose void* is the same as the one passed in @@ -122,7 +122,7 @@ void* osrfListRemove( osrfList* list, int position ); @param addr The pointer connected to the list item we're to find @return the index of the item, or -1 if the item was not found */ -int osrfListFind( osrfList* list, void* addr ); +int osrfListFind( const osrfList* list, void* addr ); void __osrfListSetSize( osrfList* list ); @@ -131,7 +131,7 @@ void __osrfListSetSize( osrfList* list ); /** @return The number of non-null items in the list */ -unsigned int osrfListGetCount( osrfList* list ); +unsigned int osrfListGetCount( const osrfList* list ); /** * May be used as a default memory freeing call -- 2.43.2