Skip to content
Merged
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
15 changes: 9 additions & 6 deletions sys/src/ape/9src/cc.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,19 @@ char *searchlib(char *, char*);
void
main(int argc, char *argv[])
{
char *s, *suf, *ccpath, *lib;
char *s, *suf, *ccpath, *lib, *prog;
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;
prog = utfrrune(argv[0], '/');
prog = prog ? prog+1 : argv[0];
Comment on lines +70 to +71

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

If argc is 0, argv[0] will be NULL, causing utfrrune to dereference a null pointer and crash. While argc is usually at least 1 in standard execution environments, adding a check for argv[0] or argc before processing the program name is a safer practice to ensure robustness.

/* cc: POSIX mode (.o objects, a.out output, clean up intermediates)
* pcc: Plan9 native mode (.$O objects, $O.out output, keep objects) */
objext = (strcmp(prog, "cc") == 0) ? "o" : ot->o;
oname = (strcmp(prog, "cc") == 0) ? "a.out" : smprint("%s.out", ot->o);
Comment on lines +74 to +75

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 strcmp(prog, "cc") check is performed twice. Consolidating this into an if-else block would improve readability and avoid redundant string comparisons. Additionally, smprint can return NULL if memory allocation fails; its return value should be checked to prevent a potential null pointer dereference later when oname is used.

	if(strcmp(prog, "cc") == 0){
		objext = "o";
		oname = "a.out";
	}else{
		objext = ot->o;
		if((oname = smprint("%s.out", ot->o)) == NULL){
			fprint(2, "out of memory\n");
			exits("mem");
		}
	}

append(&cpp, "cpp");
append(&cpp, "-D__STDC__=1"); /* ANSI says so */
append(&cpp, "-D_POSIX_SOURCE=");
Expand Down Expand Up @@ -221,8 +224,8 @@ main(int argc, char *argv[])
append(&ld, objs.strings[i]);
append(&ld, smprint("/%s/lib/ape/libap.a", ot->name));
doexec(smprint("/bin/%s", ot->ld), &ld);
if(objs.n == 1)
remove(objs.strings[0]);
if(objs.n == 1 && strcmp(objext, "o") == 0)
remove(objs.strings[0]); /* cc mode only: clean up .o intermediate */
if(gflag) {
/* post-link: convert a.out + .dwtypes sidecars to ELF64 */
memset(&dw2elf_cmd, 0, sizeof dw2elf_cmd);
Expand Down
Loading