Re: [hackers][sbase][ed] small changes

2016-12-27 Thread Laslo Hunhold
On Fri, 07 Oct 2016 16:32:53 +0300
"Ali H. Fardan"  wrote:

Hey Ali,

> O.o, whhops, that was the wrong patch, attached is the correct one.
> sorry for spamming the list.

I manually applied your changes with a few modifications, leaving out
the goto-removal. I loosely remember that there were plans to "change"
the error() semantics or whatnot, so maybe we should keep it like that
at first.
Also, let's still pass num to rematch().

Thanks for pointing out our use of strcpy()!

Cheers

Laslo

-- 
Laslo Hunhold 



Re: [hackers][sbase][ed] small changes

2016-10-07 Thread Laslo Hunhold
On Fri, 07 Oct 2016 16:32:53 +0300
"Ali H. Fardan"  wrote:

Hey Ali,

> O.o, whhops, that was the wrong patch, attached is the correct one.
> sorry for spamming the list.

the patch is looking good. Nice work!

Cheers

Laslo

-- 
Laslo Hunhold 



Re: [hackers][sbase][ed] small changes

2016-10-07 Thread Ali H. Fardan

O.o, whhops, that was the wrong patch, attached is the correct one.
sorry for spamming the list.diff --git a/ed.c b/ed.c
index 184ed30..feeab43 100644
--- a/ed.c
+++ b/ed.c
@@ -316,7 +316,7 @@ inject(char *s)
 }
 
 static void
-clearbuf()
+clearbuf(void)
 {
 	if (scratch)
 		close(scratch);
@@ -328,7 +328,7 @@ clearbuf()
 }
 
 static void
-setscratch()
+setscratch(void)
 {
 	int r, k;
 	char *dir;
@@ -407,7 +407,7 @@ match(int num)
 }
 
 static int
-rematch(int num)
+rematch(void)
 {
 	regoff_t off = matchs[0].rm_eo;
 
@@ -451,18 +451,14 @@ getnum(void)
 
 	for (ln = 0; isdigit(c = input()); ln += n) {
 		if (ln > INT_MAX/10)
-			goto invalid;
+			error("invalid address");
 		n = c - '0';
 		ln *= 10;
 		if (INT_MAX - ln < n)
-			goto invalid;
+			error("invalid address");
 	}
 	back(c);
 	return ln;
-
-invalid:
-	error("invalid address");
-	return -1; /* not reached */
 }
 
 static int
@@ -525,7 +521,7 @@ address(int *line)
 		num = isdigit(back(input())) ? getnum() : 1;
 		num *= sign;
 		if (INT_MAX - ln < num)
-			goto invalid;
+			error("invalid address");
 		ln += num;
 	}
 	back(c);
@@ -534,14 +530,10 @@ address(int *line)
 		error("invalid address");
 	*line = ln;
 	return 1;
-
-invalid:
-	error("invalid address");
-	return -1; /* not reached */
 }
 
 static void
-getlst()
+getlst(void)
 {
 	int ln, c;
 
@@ -589,7 +581,7 @@ deflines(int def1, int def2)
 }
 
 static void
-dowrite(char *fname, int trunc)
+dowrite(const char *fname, int trunc)
 {
 	FILE *fp;
 	int i, line;
@@ -604,13 +596,14 @@ dowrite(char *fname, int trunc)
 	curln = line2;
 	if (fclose(fp))
 		error("input/output error");
-	strcpy(savfname, fname);
+	if (strlcpy(savfname, fname, sizeof(savfname)) >= sizeof(savfname))
+		error("file name too long");
 	modflag = 0;
 	curln = line;
 }
 
 static void
-doread(char *fname)
+doread(const char *fname)
 {
 	size_t cnt;
 	ssize_t n;
@@ -734,8 +727,10 @@ getfname(char comm)
 		error("file name too long");
 	} else {
 		*bp = '\0';
-		if (savfname[0] == '\0' || comm == 'e' || comm == 'f')
-			strcpy(savfname, fname);
+		if (savfname[0] == '\0' || comm == 'e' || comm == 'f') {
+			if (strlcpy(savfname, fname, sizeof(savfname)) >= sizeof(savfname))
+error("file name too long");
+		}
 		return fname;
 	}
 	return NULL; /* not reached */
@@ -1010,7 +1005,7 @@ subline(int num, int nth)
 	static size_t siz, cap;
 
 	i = changed = siz = 0;
-	for (m = match(num); m; m = rematch(num)) {
+	for (m = match(num); m; m = rematch()) {
 		addpre(, , );
 		changed |= addsub(, , , nth, ++i);
 		if (eol || bol)


Re: [hackers][sbase][ed] small changes

2016-10-07 Thread Ali H. Fardan
Uhm, sorry about that, but my last patch violates the default ed 
behavior

by using eprintf(), here is the corrected version:

diff --git a/ed.c b/ed.c
index 184ed30..1e834a6 100644
--- a/ed.c
+++ b/ed.c
@@ -316,7 +316,7 @@ inject(char *s)
 }

 static void
-clearbuf()
+clearbuf(void)
 {
if (scratch)
close(scratch);
@@ -328,7 +328,7 @@ clearbuf()
 }

 static void
-setscratch()
+setscratch(void)
 {
int r, k;
char *dir;
@@ -407,7 +407,7 @@ match(int num)
 }

 static int
-rematch(int num)
+rematch(void)
 {
regoff_t off = matchs[0].rm_eo;

@@ -451,18 +451,14 @@ getnum(void)

for (ln = 0; isdigit(c = input()); ln += n) {
if (ln > INT_MAX/10)
-   goto invalid;
+   error("invalid address");
n = c - '0';
ln *= 10;
if (INT_MAX - ln < n)
-   goto invalid;
+   error("invalid address");
}
back(c);
return ln;
-
-invalid:
-   error("invalid address");
-   return -1; /* not reached */
 }

 static int
@@ -525,7 +521,7 @@ address(int *line)
num = isdigit(back(input())) ? getnum() : 1;
num *= sign;
if (INT_MAX - ln < num)
-   goto invalid;
+   error("invalid address");
ln += num;
}
back(c);
@@ -534,14 +530,10 @@ address(int *line)
error("invalid address");
*line = ln;
return 1;
-
-invalid:
-   error("invalid address");
-   return -1; /* not reached */
 }

 static void
-getlst()
+getlst(void)
 {
int ln, c;

@@ -589,7 +581,7 @@ deflines(int def1, int def2)
 }

 static void
-dowrite(char *fname, int trunc)
+dowrite(const char *fname, int trunc)
 {
FILE *fp;
int i, line;
@@ -604,13 +596,14 @@ dowrite(char *fname, int trunc)
curln = line2;
if (fclose(fp))
error("input/output error");
-   strcpy(savfname, fname);
+   if (strlcpy(savfname, fname, sizeof(savfname)))
+   error("file name too long");
modflag = 0;
curln = line;
 }

 static void
-doread(char *fname)
+doread(const char *fname)
 {
size_t cnt;
ssize_t n;
@@ -734,8 +727,10 @@ getfname(char comm)
error("file name too long");
} else {
*bp = '\0';
-   if (savfname[0] == '\0' || comm == 'e' || comm == 'f')
-   strcpy(savfname, fname);
+   if (savfname[0] == '\0' || comm == 'e' || comm == 'f') {
+   if (strlcpy(savfname, fname, sizeof(savfname)))
+   error("file name too long");
+   }
return fname;
}
return NULL; /* not reached */
@@ -1010,7 +1005,7 @@ subline(int num, int nth)
static size_t siz, cap;

i = changed = siz = 0;
-   for (m = match(num); m; m = rematch(num)) {
+   for (m = match(num); m; m = rematch()) {
addpre(, , );
changed |= addsub(, , , nth, ++i);
if (eol || bol)
diff --git a/ed.c b/ed.c
index 184ed30..1e834a6 100644
--- a/ed.c
+++ b/ed.c
@@ -316,7 +316,7 @@ inject(char *s)
 }
 
 static void
-clearbuf()
+clearbuf(void)
 {
 	if (scratch)
 		close(scratch);
@@ -328,7 +328,7 @@ clearbuf()
 }
 
 static void
-setscratch()
+setscratch(void)
 {
 	int r, k;
 	char *dir;
@@ -407,7 +407,7 @@ match(int num)
 }
 
 static int
-rematch(int num)
+rematch(void)
 {
 	regoff_t off = matchs[0].rm_eo;
 
@@ -451,18 +451,14 @@ getnum(void)
 
 	for (ln = 0; isdigit(c = input()); ln += n) {
 		if (ln > INT_MAX/10)
-			goto invalid;
+			error("invalid address");
 		n = c - '0';
 		ln *= 10;
 		if (INT_MAX - ln < n)
-			goto invalid;
+			error("invalid address");
 	}
 	back(c);
 	return ln;
-
-invalid:
-	error("invalid address");
-	return -1; /* not reached */
 }
 
 static int
@@ -525,7 +521,7 @@ address(int *line)
 		num = isdigit(back(input())) ? getnum() : 1;
 		num *= sign;
 		if (INT_MAX - ln < num)
-			goto invalid;
+			error("invalid address");
 		ln += num;
 	}
 	back(c);
@@ -534,14 +530,10 @@ address(int *line)
 		error("invalid address");
 	*line = ln;
 	return 1;
-
-invalid:
-	error("invalid address");
-	return -1; /* not reached */
 }
 
 static void
-getlst()
+getlst(void)
 {
 	int ln, c;
 
@@ -589,7 +581,7 @@ deflines(int def1, int def2)
 }
 
 static void
-dowrite(char *fname, int trunc)
+dowrite(const char *fname, int trunc)
 {
 	FILE *fp;
 	int i, line;
@@ -604,13 +596,14 @@ dowrite(char *fname, int trunc)
 	curln = line2;
 	if (fclose(fp))
 		error("input/output error");
-	strcpy(savfname, fname);
+	if (strlcpy(savfname, fname, sizeof(savfname)))
+		error("file name too long");
 	modflag = 0;
 	curln = line;
 }
 
 static void
-doread(char *fname)
+doread(const char *fname)
 {
 	size_t cnt;
 	ssize_t n;
@@ -734,8 +727,10 @@ getfname(char