shithub: riscv

Download patch

ref: d332f8a9b5d4437e7d8d0946bb03c13d7fe8a236
parent: 1c4c82277ea368739af6a4666ae530bbcfae71cb
author: Ori Bernstein <ori@eigenstate.org>
date: Wed May 13 04:42:00 EDT 2020

fix yacc crash with absolute paths

When passing an absolute file path to yacc, we would skip
initializing inpath, leaving it null. This would cause Bopen
to die. We would similarly fail to report an error if we tried
to get the current working directory, and then die when
constructing inpath.

This fixes both cases.

--- a/sys/src/cmd/yacc.c
+++ b/sys/src/cmd/yacc.c
@@ -1185,7 +1185,7 @@
 	long c, t;
 	int i, j, lev, ty, ytab, *p;
 	int vflag, dflag, stem;
-	char actnm[8], *stemc, *s, dirbuf[128];
+	char actnm[8], *stemc, dirbuf[512];
 
 	ytab = 0;
 	vflag = 0;
@@ -1230,16 +1230,14 @@
 	Blethal(faction, nil);
 	if(argc < 1)
 		error("no input file");
+
+	dirbuf[0] = '\0';
 	infile = argv[0];
-	if(infile[0] != '/' && getwd(dirbuf, sizeof dirbuf)!=nil){
-		i = strlen(infile)+1+strlen(dirbuf)+1+10;
-		s = malloc(i);
-		if(s != nil){
-			snprint(s, i, "%s/%s", dirbuf, infile);
-			cleanname(s);
-			inpath = s;
-		}
-	}
+	if(infile[0] != '/' && getwd(dirbuf, sizeof dirbuf)==nil)
+		error("cannot get cwd");
+	inpath = smprint("%s/%s", dirbuf, infile);
+	cleanname(inpath);
+
 	finput = Bopen(inpath, OREAD);
 	if(finput == 0)
 		error("cannot open '%s'", argv[0]);