Fix several bugs in SFSFindDataFromSavedOrSave/SFSAddSaved interface
authorteodor <teodor>
Wed, 24 Dec 2008 15:53:04 +0000 (15:53 +0000)
committerteodor <teodor>
Wed, 24 Dec 2008 15:53:04 +0000 (15:53 +0000)
sfxstr.c

index e9bc84a..6bf215d 100644 (file)
--- a/sfxstr.c
+++ b/sfxstr.c
@@ -155,7 +155,7 @@ SFSFindData(SFSTree *info, char *word, int len) {
 void*
 SFSFindDataOrSave(SFSTree *info, SFSDataIO *in, SFSTreePosition *position) {
        if ( position )
 void*
 SFSFindDataOrSave(SFSTree *info, SFSDataIO *in, SFSTreePosition *position) {
        if ( position )
-               memset(position, 0, sizeof(position));
+               memset(position, 0, sizeof(SFSTreePosition));
 
        return SFSFindDataFromSavedOrSave(info, in, position); 
 }
 
        return SFSFindDataFromSavedOrSave(info, in, position); 
 }
@@ -185,11 +185,13 @@ SFSFindDataFromSavedOrSave(SFSTree *info, SFSDataIO *in, SFSTreePosition *positi
                                return NULL;
                        else if ( STRNCMP(ptr, ((char*)node)+node->dataptr, node->nchar) ) {
                                ptr+=node->nchar;
                                return NULL;
                        else if ( STRNCMP(ptr, ((char*)node)+node->dataptr, node->nchar) ) {
                                ptr+=node->nchar;
-                               if ( ISEND(ptr, in->key, in->keylen) && node->isword) {
-                                       return (void*) ( ((char*)(node->data)) + ((node->haschild) ? sizeof(SFSNode*) : 0) );
+                               if ( ISEND(ptr, in->key, in->keylen) ) {
+                                       if (node->isword)
+                                               return (void*) ( ((char*)(node->data)) + ((node->haschild) ? sizeof(SFSNode*) : 0) );
+                                       return NULL;
                                } else if ( node->haschild ) {
                                } else if ( node->haschild ) {
-                                       node = getSkipChildPointer(info, node);
                                        pnode = (SFSNode**)( (char*)(node->data) );
                                        pnode = (SFSNode**)( (char*)(node->data) );
+                                       node = getSkipChildPointer(info, node);
                                } else {
                                        return NULL;
                                }
                                } else {
                                        return NULL;
                                }
@@ -202,11 +204,13 @@ SFSFindDataFromSavedOrSave(SFSTree *info, SFSDataIO *in, SFSTreePosition *positi
                                StopMiddle = StopLow + ((StopHigh - StopLow) >> 1);
                                if ( StopMiddle->val == *ptr ) {
                                        ptr++;
                                StopMiddle = StopLow + ((StopHigh - StopLow) >> 1);
                                if ( StopMiddle->val == *ptr ) {
                                        ptr++;
-                                       if ( ISEND(ptr, in->key, in->keylen) && StopMiddle->isword ) {
-                                               return (void*)( ((char*)node) + node->dataptr + info->datasize * StopMiddle->data );
+                                       if ( ISEND(ptr, in->key, in->keylen) ) {
+                                               if ( StopMiddle->isword )
+                                                       return (void*)( ((char*)node) + node->dataptr + info->datasize * StopMiddle->data );
+                                               return NULL;
                                        } else if ( StopMiddle->haschild ) {
                                        } else if ( StopMiddle->haschild ) {
-                                               node = getChildPointer(info, StopMiddle);
                                                pnode = (SFSNode**)(((char*)StopMiddle) + StopMiddle->child);
                                                pnode = (SFSNode**)(((char*)StopMiddle) + StopMiddle->child);
+                                               node = getChildPointer(info, StopMiddle);
                                        } else {
                                                return NULL;
                                        }
                                        } else {
                                                return NULL;
                                        }
@@ -228,7 +232,7 @@ void
 SFSAddSaved(SFSTree *info, SFSDataIO *in, SFSTreePosition *position) {
        CHECK_MEMORY(info);
 
 SFSAddSaved(SFSTree *info, SFSDataIO *in, SFSTreePosition *position) {
        CHECK_MEMORY(info);
 
-       if ( !(position->nodeptr && position->node) ) {
+       if ( !(position && position->nodeptr && position->node) ) {
                SFSAdd(info, in);
                return;
        }
                SFSAdd(info, in);
                return;
        }