From: Mike D. Lowis Date: Sun, 7 Apr 2013 02:02:04 +0000 (-0400) Subject: Remove null pointer checks from list (garbage-in, garbage-out) X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=6e4b385b3790d742c58cb815f285ac62632a4cb4;p=projs%2Flibcds.git Remove null pointer checks from list (garbage-in, garbage-out) --- diff --git a/source/list/sll.c b/source/list/sll.c index 02abc54..550fee2 100644 --- a/source/list/sll.c +++ b/source/list/sll.c @@ -19,126 +19,96 @@ sll_node_t* sll_new_node(void* contents) void sll_free(sll_t* list, int free_contents) { - if( NULL != list ) + sll_node_t* node = list->head; + while( NULL != node ) { - sll_node_t* node = list->head; - while( NULL != node ) - { - sll_node_t* next = node->next; - sll_free_node( node, free_contents ); - node = next; - } - free( list ); + sll_node_t* next = node->next; + sll_free_node( node, free_contents ); + node = next; } + free( list ); } void sll_free_node(sll_node_t* node, int free_contents) { - if( NULL != node ) + if( free_contents ) { - if( 1 == free_contents ) - { - free( node->contents ); - } - free( node ); + free( node->contents ); } + free( node ); } sll_node_t* sll_front( sll_t* list ) { - sll_node_t* node = NULL; - if( NULL != list ) - { - node = list->head; - } - return node; + return list->head; } sll_node_t* sll_back( sll_t* list ) { - sll_node_t* node = NULL; - if( NULL != list ) - { - node = list->tail; - } - return node; + return list->tail; } unsigned int sll_size(sll_t* list) { unsigned int length = 0; - if( NULL != list) + sll_node_t* node = list->head; + while( NULL != node ) { - sll_node_t* node = list->head; - while( NULL != node ) - { - node = node->next; - length++; - } + node = node->next; + length++; } return length; } int sll_empty(sll_t* list) { - return ((NULL == list) || ((NULL == list->head) && (NULL == list->tail))); + return ((NULL == list->head) && (NULL == list->tail)); } sll_node_t* sll_at(sll_t* list, unsigned int index) { sll_node_t* node = NULL; - if( NULL != list ) + unsigned int cur_index = 0; + sll_node_t* cur_node = list->head; + while( NULL != cur_node ) { - unsigned int cur_index = 0; - sll_node_t* cur_node = list->head; - while( NULL != cur_node ) + if( cur_index == index ) { - if( cur_index == index ) - { - node = cur_node; - break; - } - cur_node = cur_node->next; - cur_index++; + node = cur_node; + break; } + cur_node = cur_node->next; + cur_index++; } return node; } sll_node_t* sll_push_front( sll_t* list, void* contents ) { - sll_node_t* node = NULL; - if( NULL != list ) + sll_node_t* node = sll_new_node( contents ); + node->next = list->head; + list->head = node; + if( NULL == list->tail ) { - node = sll_new_node( contents ); - node->next = list->head; - list->head = node; - if( NULL == list->tail ) - { - list->tail = node; - } + list->tail = node; } return node; } sll_node_t* sll_push_back( sll_t* list, void* contents ) { - sll_node_t* node = NULL; - if( NULL != list ) + sll_node_t* node = sll_new_node( contents ); + node->next = NULL; + if( NULL == list->tail ) { - node = sll_new_node( contents ); - node->next = NULL; - if( NULL == list->tail ) - { - list->head = node; - list->tail = node; - } - else - { - list->tail->next = node; - list->tail = node; - } + list->head = node; + list->tail = node; + } + else + { + list->tail->next = node; + list->tail = node; } return node; } @@ -146,7 +116,7 @@ sll_node_t* sll_push_back( sll_t* list, void* contents ) sll_node_t* sll_pop_front( sll_t* list ) { sll_node_t* node = NULL; - if( (NULL != list) && (NULL != list->head) ) + if( NULL != list->head ) { node = list->head; list->head = node->next; @@ -161,25 +131,22 @@ sll_node_t* sll_pop_front( sll_t* list ) sll_node_t* sll_pop_back( sll_t* list ) { sll_node_t* node = NULL; - if( NULL != list ) + if ( list->head == list->tail ) { - if ( list->head == list->tail ) - { - node = list->head; - list->head = NULL; - list->tail = NULL; - } - else + node = list->head; + list->head = NULL; + list->tail = NULL; + } + else + { + sll_node_t* next_tail = list->head; + while( next_tail->next != list->tail ) { - sll_node_t* next_tail = list->head; - while( next_tail->next != list->tail ) - { - next_tail = next_tail->next; - } - node = next_tail->next; - next_tail->next = NULL; - list->tail = next_tail; + next_tail = next_tail->next; } + node = next_tail->next; + next_tail->next = NULL; + list->tail = next_tail; } return node; } @@ -243,19 +210,15 @@ sll_node_t* sll_delete( sll_t* list, unsigned int index, int free_contents) sll_t* sll_clear(sll_t* list, int free_contents) { - if(NULL != list) + sll_node_t* node = list->head; + while(NULL != node) { - sll_node_t* node = list->head; - while(NULL != node) - { - sll_node_t* next = node->next; - sll_free_node(node,free_contents); - node = next; - } - list->head = NULL; - list->tail = NULL; + sll_node_t* next = node->next; + sll_free_node(node,free_contents); + node = next; } + list->head = NULL; + list->tail = NULL; return list; } - diff --git a/tests/test_sll.cpp b/tests/test_sll.cpp index a776a5e..a1bce50 100644 --- a/tests/test_sll.cpp +++ b/tests/test_sll.cpp @@ -39,11 +39,6 @@ namespace { //------------------------------------------------------------------------- // Test sll_free function //------------------------------------------------------------------------- - TEST(Verify_sll_free_does_nothing_on_null_pointer) - { - sll_free( NULL, 0 ); - } - TEST(Verify_sll_free_frees_the_given_empty_list) { sll_t* list = sll_new(); @@ -70,11 +65,6 @@ namespace { //------------------------------------------------------------------------- // Test sll_free_node function //------------------------------------------------------------------------- - TEST(Verify_sll_free_node_does_nothing_on_null_pointer) - { - sll_free_node( NULL, 0 ); - } - TEST(Verify_sll_free_node_frees_the_given_node) { sll_node_t* node = sll_new_node( NULL ); @@ -91,11 +81,6 @@ namespace { //------------------------------------------------------------------------- // Test sll_front function //------------------------------------------------------------------------- - TEST(Verify_sll_front_returns_NULL_if_list_is_NULL) - { - CHECK( NULL == sll_front( NULL ) ); - } - TEST(Verify_sll_front_returns_NULL_if_list_is_empty) { sll_t list = { NULL, NULL }; @@ -113,11 +98,6 @@ namespace { //------------------------------------------------------------------------- // Test sll_back function //------------------------------------------------------------------------- - TEST(Verify_sll_back_returns_NULL_if_list_is_NULL) - { - CHECK( NULL == sll_back( NULL ) ); - } - TEST(Verify_sll_back_returns_NULL_if_list_is_empty) { sll_t list = { NULL, NULL }; @@ -135,11 +115,6 @@ namespace { //------------------------------------------------------------------------- // Test sll_size function //------------------------------------------------------------------------- - TEST(Verify_sll_size_returns_0_when_passed_null_pointer) - { - CHECK( 0 == sll_size(NULL) ); - } - TEST(Verify_sll_size_returns_0_when_list_is_empty) { sll_t* list = sll_new(); @@ -167,11 +142,6 @@ namespace { //------------------------------------------------------------------------- // Test sll_empty function //------------------------------------------------------------------------- - TEST(Verify_sll_empty_returns_true_when_passed_null_pointer) - { - CHECK( 1 == sll_empty( NULL ) ); - } - TEST(Verify_sll_empty_returns_true_when_head_and_tail_are_null) { sll_t list = { NULL, NULL }; @@ -193,11 +163,6 @@ namespace { //------------------------------------------------------------------------- // Test sll_at function //------------------------------------------------------------------------- - TEST(Verify_sll_at_returns_NULL_on_null_pointer) - { - CHECK( NULL == sll_at( NULL, 0 ) ); - } - TEST(Verify_sll_at_returns_NULL_when_list_is_empty) { sll_t list = { NULL, NULL }; @@ -243,11 +208,6 @@ namespace { //------------------------------------------------------------------------- // Test sll_push_front function //------------------------------------------------------------------------- - TEST(Verify_sll_push_front_returns_null_if_list_is_null) - { - CHECK( NULL == sll_push_front( NULL, NULL ) ); - } - TEST(Verify_sll_push_front_pushes_to_empty_list) { sll_t list = { NULL, NULL }; @@ -274,11 +234,6 @@ namespace { //------------------------------------------------------------------------- // Test sll_push_back function //------------------------------------------------------------------------- - TEST(Verify_sll_push_back_returns_null_if_list_is_null) - { - CHECK( NULL == sll_push_back( NULL, NULL ) ); - } - TEST(Verify_sll_push_back_pushes_to_empty_list) { sll_t list = { NULL, NULL }; @@ -305,11 +260,6 @@ namespace { //------------------------------------------------------------------------- // Test sll_pop_front function //------------------------------------------------------------------------- - TEST(Verify_pop_front_returns_null_if_list_is_null) - { - CHECK( NULL == sll_pop_front( NULL ) ); - } - TEST(Verify_pop_front_returns_null_if_list_is_empty) { sll_t list = { NULL, NULL }; @@ -349,11 +299,6 @@ namespace { //------------------------------------------------------------------------- // Test sll_pop_back function //------------------------------------------------------------------------- - TEST(Verify_pop_back_does_nothing_if_list_is_null) - { - CHECK( NULL == sll_pop_back( NULL ) ); - } - TEST(Verify_pop_back_does_nothing_if_list_is_empty) { sll_t list = { NULL, NULL }; @@ -393,11 +338,6 @@ namespace { //------------------------------------------------------------------------- // Test sll_insert function //------------------------------------------------------------------------- - TEST(Verify_insert_does_nothing_if_list_is_null) - { - CHECK( NULL == sll_insert( NULL, 0, (void*)0x1234 ) ); - } - TEST(Verify_insert_should_insert_into_empty_list) { sll_t list = { NULL, NULL }; @@ -459,11 +399,6 @@ namespace { //------------------------------------------------------------------------- // Test sll_delete function //------------------------------------------------------------------------- - TEST(Verify_delete_does_nothing_if_list_is_null) - { - CHECK( NULL == sll_delete( NULL, 0, 0 ) ); - } - TEST(Verify_delete_does_nothing_if_list_is_empty) { sll_t list = { NULL, NULL }; @@ -521,11 +456,6 @@ namespace { //------------------------------------------------------------------------- // Test sll_clear function //------------------------------------------------------------------------- - TEST(Verify_sll_clear_does_nothing_if_passed_a_NULL_pointer) - { - CHECK( NULL == sll_clear(NULL,0) ); - } - TEST(Verify_sll_clear_does_nothing_for_an_empty_list) { sll_t* list = sll_new();