From: Matthew Fernandez Date: Sun, 26 Jul 2020 21:01:27 +0000 (-0700) Subject: remove exception-style control flow in lib/rbtree X-Git-Tag: 2.46.0~20^2^2~168 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=15379daf5273c8bcf3ffc09f75b8e29d003f262c;p=graphviz remove exception-style control flow in lib/rbtree Fixes #1742. --- diff --git a/lib/rbtree/CMakeLists.txt b/lib/rbtree/CMakeLists.txt index bf6283af1..fc3c230a5 100644 --- a/lib/rbtree/CMakeLists.txt +++ b/lib/rbtree/CMakeLists.txt @@ -4,7 +4,6 @@ include_directories( add_library(rbtree STATIC # Header files - misc.h red_black_tree.h stack.h diff --git a/lib/rbtree/Makefile.am b/lib/rbtree/Makefile.am index 5d938f9ef..f7cf28e8c 100644 --- a/lib/rbtree/Makefile.am +++ b/lib/rbtree/Makefile.am @@ -3,7 +3,7 @@ AM_CPPFLAGS = -I$(top_srcdir) -noinst_HEADERS = misc.h red_black_tree.h stack.h +noinst_HEADERS = red_black_tree.h stack.h noinst_LTLIBRARIES = librbtree_C.la librbtree_C_la_SOURCES = misc.c red_black_tree.c stack.c diff --git a/lib/rbtree/misc.c b/lib/rbtree/misc.c index 413db21a4..2759909e8 100644 --- a/lib/rbtree/misc.c +++ b/lib/rbtree/misc.c @@ -5,39 +5,8 @@ * See the LICENSE file for copyright information. * **********************************************************/ -#include "config.h" +#include "red_black_tree.h" -#include "misc.h" -#include - -jmp_buf rb_jbuf; - -/***********************************************************************/ -/* FUNCTION: SafeMalloc */ -/**/ -/* INPUTS: size is the size to malloc */ -/**/ -/* OUTPUT: returns pointer to allocated memory if successful */ -/**/ -/* EFFECT: mallocs new memory. If malloc fails, prints error message */ -/* and terminates program. */ -/**/ -/* Modifies Input: none */ -/**/ -/***********************************************************************/ - -void * SafeMalloc(size_t size) { - void * result; - - if ( (result = malloc(size)) ) { /* assignment intentional */ - return(result); - } else { - fprintf(stderr, "memory overflow: malloc failed in SafeMalloc."); - /* printf(" Exiting Program.\n"); */ - longjmp(rb_jbuf, 2); - return(0); - } -} /* NullFunction does nothing it is included so that it can be passed */ /* as a function to RBTreeCreate when no other suitable function has */ /* been defined */ diff --git a/lib/rbtree/misc.h b/lib/rbtree/misc.h deleted file mode 100644 index 2c97a0e4f..000000000 --- a/lib/rbtree/misc.h +++ /dev/null @@ -1,42 +0,0 @@ -/* $Id$Revision: */ -/* vim:set shiftwidth=4 ts=8: */ - -/********************************************************** -* See the LICENSE file for copyright information. * -**********************************************************/ - -#ifndef INC_E_MISC_ -#define INC_E_MISC_ - -#ifdef __cplusplus -extern "C" { -#endif - -#include -#include - -extern jmp_buf rb_jbuf; - -/* CONVENTIONS: All data structures for red-black trees have the prefix */ -/* "rb_" to prevent name conflicts. */ -/* */ -/* Function names: Each word in a function name begins with */ -/* a capital letter. An example funcntion name is */ -/* CreateRedTree(a,b,c). Furthermore, each function name */ -/* should begin with a capital letter to easily distinguish */ -/* them from variables. */ -/* */ -/* Variable names: Each word in a variable name begins with */ -/* a capital letter EXCEPT the first letter of the variable */ -/* name. For example, int newLongInt. Global variables have */ -/* names beginning with "g". An example of a global */ -/* variable name is gNewtonsConstant. */ - -void * SafeMalloc(size_t size); - -#ifdef __cplusplus -} -#endif - -#endif - diff --git a/lib/rbtree/rbtree.vcxproj b/lib/rbtree/rbtree.vcxproj index 31c0ab108..687387b28 100644 --- a/lib/rbtree/rbtree.vcxproj +++ b/lib/rbtree/rbtree.vcxproj @@ -76,7 +76,6 @@ - diff --git a/lib/rbtree/rbtree.vcxproj.filters b/lib/rbtree/rbtree.vcxproj.filters index a587585dc..fa66f522f 100644 --- a/lib/rbtree/rbtree.vcxproj.filters +++ b/lib/rbtree/rbtree.vcxproj.filters @@ -15,9 +15,6 @@ - - Header Files - Header Files diff --git a/lib/rbtree/red_black_tree.c b/lib/rbtree/red_black_tree.c index c5468a7c3..f11e811bd 100644 --- a/lib/rbtree/red_black_tree.c +++ b/lib/rbtree/red_black_tree.c @@ -10,6 +10,7 @@ #include #include "red_black_tree.h" #include "stdio.h" +#include /***********************************************************************/ /* FUNCTION: RBTreeCreate */ @@ -39,14 +40,10 @@ rb_red_blk_tree* RBTreeCreate( int (*CompFunc) (const void*,const void*), rb_red_blk_tree* newTree = NULL; rb_red_blk_node* temp; - if (setjmp(rb_jbuf)) { - if (newTree) { - if (newTree->nil) free (newTree->nil); - free (newTree); - } + newTree= malloc(sizeof(rb_red_blk_tree)); + if (newTree == NULL) { return NULL; } - newTree=(rb_red_blk_tree*) SafeMalloc(sizeof(rb_red_blk_tree)); newTree->nil = newTree->root = NULL; newTree->Compare= CompFunc; newTree->DestroyKey= DestFunc; @@ -56,11 +53,20 @@ rb_red_blk_tree* RBTreeCreate( int (*CompFunc) (const void*,const void*), /* see the comment in the rb_red_blk_tree structure in red_black_tree.h */ /* for information on nil and root */ - temp=newTree->nil= (rb_red_blk_node*) SafeMalloc(sizeof(rb_red_blk_node)); + temp=newTree->nil= malloc(sizeof(rb_red_blk_node)); + if (temp == NULL) { + free(newTree); + return NULL; + } temp->parent=temp->left=temp->right=temp; temp->red=0; temp->key=0; - temp=newTree->root= (rb_red_blk_node*) SafeMalloc(sizeof(rb_red_blk_node)); + temp=newTree->root= malloc(sizeof(rb_red_blk_node)); + if (temp == NULL) { + free(newTree->nil); + free(newTree); + return NULL; + } temp->parent=temp->left=temp->right=newTree->nil; temp->key=0; temp->red=0; @@ -239,9 +245,10 @@ rb_red_blk_node * RBTreeInsert(rb_red_blk_tree* tree, void* key, void* info) { rb_red_blk_node * x; rb_red_blk_node * newNode; - if (setjmp(rb_jbuf)) + x= malloc(sizeof(rb_red_blk_node)); + if (x == NULL) { return NULL; - x=(rb_red_blk_node*) SafeMalloc(sizeof(rb_red_blk_node)); + } x->key=key; x->info=info; @@ -651,10 +658,10 @@ stk_stack* RBEnumerate(rb_red_blk_tree* tree, void* low, void* high) { rb_red_blk_node* x=tree->root->left; rb_red_blk_node* lastBest=nil; - if (setjmp(rb_jbuf)) { + enumResultStack=StackCreate(); + if (enumResultStack == NULL) { return NULL; } - enumResultStack=StackCreate(); while(nil != x) { if ( 1 == (tree->Compare(x->key,high)) ) { /* x->key > high */ x=x->left; @@ -664,7 +671,10 @@ stk_stack* RBEnumerate(rb_red_blk_tree* tree, void* low, void* high) { } } while ( (lastBest != nil) && (1 != tree->Compare(low,lastBest->key))) { - StackPush(enumResultStack,lastBest); + if (StackPush(enumResultStack,lastBest) != 0) { + StackDestroy(enumResultStack, NullFunction); + return NULL; + } lastBest=TreePredecessor(tree,lastBest); } return(enumResultStack); diff --git a/lib/rbtree/red_black_tree.h b/lib/rbtree/red_black_tree.h index 1f2fe1df8..7ad51fa12 100644 --- a/lib/rbtree/red_black_tree.h +++ b/lib/rbtree/red_black_tree.h @@ -12,7 +12,6 @@ extern "C" { #endif -#include "misc.h" #include "stack.h" /* CONVENTIONS: All data structures for red-black trees have the prefix */ diff --git a/lib/rbtree/stack.c b/lib/rbtree/stack.c index c3a05f458..9d8802038 100644 --- a/lib/rbtree/stack.c +++ b/lib/rbtree/stack.c @@ -8,6 +8,7 @@ #include "config.h" #include "stack.h" +#include intptr_t StackNotEmpty(stk_stack * theStack) { return( theStack ? (intptr_t) theStack->top : 0); @@ -28,28 +29,38 @@ stk_stack * StackJoin(stk_stack * stack1, stk_stack * stack2) { stk_stack * StackCreate() { stk_stack * newStack; - newStack=(stk_stack *) SafeMalloc(sizeof(stk_stack)); + newStack= malloc(sizeof(stk_stack)); + if (newStack == NULL) { + return NULL; + } newStack->top=newStack->tail=NULL; return(newStack); } -void StackPush(stk_stack * theStack, DATA_TYPE newInfoPointer) { +int StackPush(stk_stack * theStack, DATA_TYPE newInfoPointer) { stk_stack_node * newNode; if(!theStack->top) { - newNode=(stk_stack_node *) SafeMalloc(sizeof(stk_stack_node)); + newNode= malloc(sizeof(stk_stack_node)); + if (newNode == NULL) { + return -1; + } newNode->info=newInfoPointer; newNode->next=theStack->top; theStack->top=newNode; theStack->tail=newNode; } else { - newNode=(stk_stack_node *) SafeMalloc(sizeof(stk_stack_node)); + newNode= malloc(sizeof(stk_stack_node)); + if (newNode == NULL) { + return -1; + } newNode->info=newInfoPointer; newNode->next=theStack->top; theStack->top=newNode; } - + + return 0; } DATA_TYPE StackPop(stk_stack * theStack) { diff --git a/lib/rbtree/stack.h b/lib/rbtree/stack.h index 533ae55a5..ea5d33700 100644 --- a/lib/rbtree/stack.h +++ b/lib/rbtree/stack.h @@ -16,8 +16,6 @@ extern "C" { #include "config.h" #include -#include "misc.h" - /* CONVENTIONS: All data structures for stacks have the prefix */ /* "stk_" to prevent name conflicts. */ /* */ @@ -55,7 +53,15 @@ typedef struct stk_stack { /* I didn't think additional comments would be useful */ stk_stack * StackJoin(stk_stack * stack1, stk_stack * stack2); stk_stack * StackCreate(void); -void StackPush(stk_stack * theStack, DATA_TYPE newInfoPointer); + +/** push an item onto the stack + * + * @param theStack Stack to operate on + * @param newInfoPointer Item to push onto the top + * @return 0 on success + */ +int StackPush(stk_stack * theStack, DATA_TYPE newInfoPointer); + void * StackPop(stk_stack * theStack); intptr_t StackNotEmpty(stk_stack *);