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 infinite def write #316

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
16 changes: 13 additions & 3 deletions lef/defWrite.c
Copy link
Contributor

@dlmiles dlmiles Oct 8, 2024

Choose a reason for hiding this comment

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

Consider this slightl tweak to approach, outName can be NULL (according to function docs), the lefFileOpen() will return the actual filename used (after resolving what it will be, when not provided via outName). So you can't do things like strlen(NULL).

The codebase doesn't really have NULL checks on failed mallocMagic() calls, so this mitigated to ASSERT and removes the whole what to do when it fails consideration the if() block implies.

I'm assuming the filename contains the file extension (.def/.lef) as it is used directly with unlink() as-is.

So this moves the block to be lower down around line 2986 of the original file.

    filename1 = StrDup((char **)NULL, filename); // from original file line 2985

    char *outName_part;
    {   /* use the 'filename' given back from lefFileOpen() */
        outName_part = mallocMagic(strlen(filename)+strlen("_part")+1);
        ASSERT(outName_part, "outName_part");
        strcpy(outName_part, filename);
        char *cp = strrchr(outName_part, '.');
        if (cp)
           *cp = '\0'; /* remove extn */
        strcat(outName_part, "_part");
    }

    defWriteHeader(def, f, scale, units); // from original file line 2987 (remove this comment)

Original file line number Diff line number Diff line change
Expand Up @@ -2966,6 +2966,13 @@ DefWriteCell(def, outName, allSpecial, units, analRetentive)
TxError("Please name the cell before generating DEF.\n");
return;
}
char * outName_part ;
if((outName_part = mallocMagic(strlen(outName)+strlen("_part")+1)) != NULL) {
outName_part[0] = '\0';
strcat(outName_part, outName);
strcat(outName_part, "_part");
}
//

f = lefFileOpen(def, outName, ".def", "w", &filename);

Expand Down Expand Up @@ -3016,7 +3023,7 @@ DefWriteCell(def, outName, allSpecial, units, analRetentive)

/* Not done yet with output, so keep this file open. . . */

f2 = lefFileOpen(def, outName, ".def_part", "w", &filename);
f2 = lefFileOpen(def, outName_part, ".def", "w", &filename);

if (f2 == NULL)
{
Expand All @@ -3030,7 +3037,8 @@ DefWriteCell(def, outName, allSpecial, units, analRetentive)
/* If part 2 cannot be opened, remove part 1 */
fclose(f);
unlink(filename1);
freeMagic(filename1);
freeMagic(filename1);
freeMagic(outName_part);
return;
}
filename2 = StrDup((char **)NULL, filename);
Expand Down Expand Up @@ -3133,7 +3141,7 @@ DefWriteCell(def, outName, allSpecial, units, analRetentive)

/* Append contents of file with NETS and SPECIALNETS sections */

f2 = lefFileOpen(def, outName, ".def_part", "r", &filename);
f2 = lefFileOpen(def, outName_part, ".def", "r", &filename);
if (f2 == NULL)
{
/* This should not happen because the file was just written. . . */
Expand All @@ -3149,10 +3157,12 @@ DefWriteCell(def, outName, allSpecial, units, analRetentive)
unlink(filename1);
freeMagic(filename1);
freeMagic(filename2);
freeMagic(outName_part);
return;
}
while (fgets(line, sizeof line, f2) != NULL) fprintf(f, "%s", line);
fclose(f2);
freeMagic(outName_part);

/* Blockages */
defWriteBlockages(f, def, scale, lefMagicToLefLayer);
Expand Down