From c39a501c3e58eeae83dc4ad9f6a683b2d7e1bf3d Mon Sep 17 00:00:00 2001 From: Henner Zeller Date: Mon, 21 Nov 2022 12:42:39 -0800 Subject: [PATCH] Fix undefined behavior: invalid access of NULL ptr. Found with msan/asan analysis. Signed-off-by: Henner Zeller --- src/base/main/libSupport.c | 12 +++++++----- src/map/if/ifMan.c | 18 +++++++++++++----- src/misc/extra/extraUtilUtil.c | 10 +++++++--- 3 files changed, 27 insertions(+), 13 deletions(-) diff --git a/src/base/main/libSupport.c b/src/base/main/libSupport.c index 4d76241338..6364963f5a 100644 --- a/src/base/main/libSupport.c +++ b/src/base/main/libSupport.c @@ -74,12 +74,10 @@ void open_libs() { } // Extract directories and read libraries - done = 0; p = init_p; - while (!done) { + for (;;) { char *endp = strchr (p,':'); - if (endp == NULL) done = 1; // last directory in the list - else *endp = 0; // end of string + if (endp != NULL) *endp = 0; // end of string dirp = opendir(p); if (dirp == NULL) { @@ -119,7 +117,11 @@ void open_libs() { } } closedir(dirp); - p = endp+1; + if (endp == NULL) { + break; // last directory in the list + } else { + p = endp+1; + } } ABC_FREE(init_p); diff --git a/src/map/if/ifMan.c b/src/map/if/ifMan.c index 6ecd0eb88d..027a35a419 100644 --- a/src/map/if/ifMan.c +++ b/src/map/if/ifMan.c @@ -96,11 +96,19 @@ If_Man_t * If_ManStart( If_Par_t * pPars ) Abc_Print( 1, "K = %d. Memory (bytes): Truth = %4d. Cut = %4d. Obj = %4d. Set = %4d. CutMin = %s\n", p->pPars->nLutSize, 8 * p->nTruth6Words[p->pPars->nLutSize], p->nCutBytes, p->nObjBytes, p->nSetBytes, p->pPars->fCutMin? "yes":"no" ); // room for temporary truth tables - p->puTemp[0] = p->pPars->fTruth? ABC_ALLOC( unsigned, 8 * p->nTruth6Words[p->pPars->nLutSize] ) : NULL; - p->puTemp[1] = p->puTemp[0] + p->nTruth6Words[p->pPars->nLutSize]*2; - p->puTemp[2] = p->puTemp[1] + p->nTruth6Words[p->pPars->nLutSize]*2; - p->puTemp[3] = p->puTemp[2] + p->nTruth6Words[p->pPars->nLutSize]*2; - p->puTempW = p->pPars->fTruth? ABC_ALLOC( word, p->nTruth6Words[p->pPars->nLutSize] ) : NULL; + if ( p->pPars->fTruth ) + { + p->puTemp[0] = p->pPars->fTruth? ABC_ALLOC( unsigned, 8 * p->nTruth6Words[p->pPars->nLutSize] ) : NULL; + p->puTemp[1] = p->puTemp[0] + p->nTruth6Words[p->pPars->nLutSize]*2; + p->puTemp[2] = p->puTemp[1] + p->nTruth6Words[p->pPars->nLutSize]*2; + p->puTemp[3] = p->puTemp[2] + p->nTruth6Words[p->pPars->nLutSize]*2; + p->puTempW = p->pPars->fTruth? ABC_ALLOC( word, p->nTruth6Words[p->pPars->nLutSize] ) : NULL; + } + else + { + p->puTemp[0] = p->puTemp[1] = p->puTemp[2] = p->puTemp[3] = NULL; + p->puTempW = NULL; + } if ( pPars->fUseDsd ) { for ( v = 6; v <= Abc_MaxInt(6,p->pPars->nLutSize); v++ ) diff --git a/src/misc/extra/extraUtilUtil.c b/src/misc/extra/extraUtilUtil.c index 2f0f4559b4..7a9e8fd0b8 100644 --- a/src/misc/extra/extraUtilUtil.c +++ b/src/misc/extra/extraUtilUtil.c @@ -280,7 +280,7 @@ char * Extra_UtilFileSearch(char *file, char *path, char *mode) save_path = path = Extra_UtilStrsav(path); quit = 0; - do { + for (;;) { cp = strchr(path, ':'); if (cp != 0) { *cp = '\0'; @@ -304,8 +304,12 @@ char * Extra_UtilFileSearch(char *file, char *path, char *mode) return filename; } ABC_FREE(filename); - path = ++cp; - } while (! quit); + if (quit) { + break; + } else { + path = ++cp; + } + } ABC_FREE(save_path); return 0;