Skip to content
Merged
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
10 changes: 6 additions & 4 deletions sys/src/ape/9src/cc.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,15 @@ void
main(int argc, char *argv[])
{
char *s, *suf, *ccpath, *lib;
char *oname;
char *oname, *objext;
int haveoname = 0;
int i, cppn, ccn;
Objtype *ot;

ot = findoty();
oname = "a.out";
/* cc produces .o (for configure compat); pcc uses arch-native .$O */
objext = (strcmp(argv[0], "cc") == 0) ? "o" : ot->o;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The check strcmp(argv[0], "cc") is fragile because argv[0] may contain the full or relative path to the executable (e.g., /bin/cc or ./cc). In such cases, the condition will fail, and the driver will use the architecture-native extension instead of .o, which defeats the purpose of the "configure compat" mode when the tool is invoked via its path. It is more robust to check the base name of the command (e.g., using strrchr(argv[0], '/')).

append(&cpp, "cpp");
append(&cpp, "-D__STDC__=1"); /* ANSI says so */
append(&cpp, "-D_POSIX_SOURCE=");
Expand Down Expand Up @@ -187,7 +189,7 @@ main(int argc, char *argv[])
cppn = cpp.n;
ccn = cc.n;
if(gflag)
putenv("_DWTYPES=1");
putenv("_DWTYPES", "1");
for(i = 0; i < srcs.n; i++) {
append(&cpp, srcs.strings[i]);
if(Eflag)
Expand All @@ -200,15 +202,15 @@ main(int argc, char *argv[])
if (haveoname && cflag)
append(&cc, oname);
else
append(&cc, changeext(srcs.strings[i], "o"));
append(&cc, changeext(srcs.strings[i], objext));
}
dopipe("/bin/cpp", &cpp, ccpath, &cc);
}
cpp.n = cppn;
cc.n = ccn;
}
if(gflag)
unsetenv("_DWTYPES");
putenv("_DWTYPES", "");

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

In the Plan 9 environment (which the 2-argument putenv call implies), putenv(name, "") sets the environment variable to an empty string rather than removing it. Since getenv() returns a non-NULL pointer for empty strings, any subsequent code (such as the linker called on line 214) that checks for the existence of _DWTYPES will still see it as set. To properly unset the variable so that getenv returns NULL, you should pass (char*)0 (or nil) as the second argument.

		putenv("_DWTYPES", (char*)0);

if(!cflag) {
List dw2elf_cmd;
append(&ld, "-o");
Expand Down
6 changes: 6 additions & 0 deletions sys/src/ape/9src/mkfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,9 @@ TARG=\

BIN=$APEXPROOT/$objtype/bin/ape
<$APEXPROOT/sys/src/cmd/mkmany

# pcc: same binary as cc; argv[0]=="pcc" selects .$O object extension
$BIN/pcc: $BIN/cc
cp $BIN/cc $BIN/pcc

install:V: $BIN/cc $BIN/pcc
Loading