Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix undefined behavior: invalid access of NULL ptr. #197

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/base/main/libSupport.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down
18 changes: 13 additions & 5 deletions src/map/if/ifMan.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Comment on lines +99 to +111
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would welcome a somewhat careful review of this change. It wasn't immediately clear to me if in the case of !p->pPars->fTruth the various p->pu* pointers are actually unused. This case currently has undefined behaviour, but maybe there's some obscure pointer-to-offset logic somewhere else that actually needed those "null pointer increments"?

if ( pPars->fUseDsd )
{
for ( v = 6; v <= Abc_MaxInt(6,p->pPars->nLutSize); v++ )
Expand Down
10 changes: 7 additions & 3 deletions src/misc/extra/extraUtilUtil.c
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same mistake fixed twice in this patch: the increment must not happen after the last round of the loop.

}
}

ABC_FREE(save_path);
return 0;
Expand Down