[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: snprintf improvements for mg



Han Boetes wrote:
> Index: buffer.c
> ===================================================================
> RCS file: /cvs/src/usr.bin/mg/buffer.c,v
> retrieving revision 1.50
> diff -u -p -r1.50 buffer.c
> --- buffer.c	2005/10/14 19:46:46	1.50
> +++ buffer.c	2005/11/12 19:10:05
> @@ -426,13 +426,12 @@ anycb(int f)
>  {
>  	BUFFER *bp;
>  	int	s = FALSE, save = FALSE;
> -	char	prompt[NFILEN + 11];
> +	char	prompt[NFILEN + 11] = "Save file ";
>  
>  	for (bp = bheadp; bp != NULL; bp = bp-> b_bufp) {
>  		if (bp-> b_fname != NULL && *(bp->b_fname) != '\0' &&
>  		    (bp-> b_flag & BFCHG) != 0) {
> -			snprintf(prompt, sizeof(prompt), "Save file %s",
> -			    bp-> b_fname);
> +			strlcat(prompt, bp-> b_fname, sizeof(prompt));
>  			if ((f == TRUE || (save = eyorn(prompt)) == TRUE) &&
>  			    buffsave(bp) == TRUE) {
>  				bp-> b_flag &= ~BFCHG;

OK, that was not such a good idea after all. Here is an alternative.

Index: buffer.c
===================================================================
RCS file: /cvs/src/usr.bin/mg/buffer.c,v
retrieving revision 1.50
diff -u -p -r1.50 buffer.c
--- buffer.c	2005/10/14 19:46:46	1.50
+++ buffer.c	2005/11/12 21:37:06
@@ -417,22 +417,26 @@ addlinef(BUFFER *bp, char *fmt, ...)
 
 /*
  * Look through the list of buffers, giving the user a chance to save them.
- * Return TRUE if there are any changed buffers afterwards.  Buffers that
- * don't have an associated file don't count.  Return FALSE if there are
- * no changed buffers.
+ * Return TRUE if there are any changed buffers afterwards.  Buffers that don't
+ * have an associated file don't count.  Return FALSE if there are no changed
+ * buffers.  Return ABORT if an error occurs or if the user presses c-g.
  */
 int
 anycb(int f)
 {
 	BUFFER *bp;
-	int	s = FALSE, save = FALSE;
+	int	s = FALSE, save = FALSE, ret;
 	char	prompt[NFILEN + 11];
 
 	for (bp = bheadp; bp != NULL; bp = bp->b_bufp) {
 		if (bp->b_fname != NULL && *(bp->b_fname) != '\0' &&
 		    (bp->b_flag & BFCHG) != 0) {
-			snprintf(prompt, sizeof(prompt), "Save file %s",
+			ret = snprintf(prompt, sizeof(prompt), "Save file %s",
 			    bp->b_fname);
+			if (ret < 0 || ret >= sizeof(prompt)) {
+				ewprintf("Error: filename too long!");
+				return (ABORT);
+			}
 			if ((f == TRUE || (save = eyorn(prompt)) == TRUE) &&
 			    buffsave(bp) == TRUE) {
 				bp->b_flag &= ~BFCHG;
Index: dired.c
===================================================================
RCS file: /cvs/src/usr.bin/mg/dired.c,v
retrieving revision 1.29
diff -u -p -r1.29 dired.c
--- dired.c	2005/11/12 20:13:47	1.29
+++ dired.c	2005/11/12 21:37:06
@@ -590,7 +590,7 @@ dired_(char *dirname)
 	BUFFER	*bp;
 	FILE	*dirpipe;
 	char	 line[256];
-	int	 len;
+	int	 len, ret;
 
 	if ((dirname = adjustname(dirname)) == NULL) {
 		ewprintf("Bad directory name");
@@ -609,8 +609,8 @@ dired_(char *dirname)
 	if (bclear(bp) != TRUE)
 		return (NULL);
 	bp->b_flag |= BFREADONLY;
-	if (snprintf(line, sizeof(line), "ls -al %s", dirname)
-	    >= sizeof(line)) {
+	ret = snprintf(line, sizeof(line), "ls -al %s", dirname);
+	if (ret < 0 || ret  >= sizeof(line)) {
 		ewprintf("Path too long");
 		return (NULL);
 	}



# Han



Visit your host, monkey.org