On Mon, 2022-01-10 at 08:21 -0700, Todd C. Miller wrote:
> On Mon, 10 Jan 2022 15:23:42 +0100, Martijn van Duren wrote:
> 
> > The lputs case is fairly straight forward and I'd like to get an OK
> > for that part.
> 
> I agree that fixing lputs() to honor psl is the best approach.
> Wouldn't the diff be simpler if you left the handling of 's' as-is
> but make the loop invariant l > 0 and decrement l accordingly?
> Using len instead of l is probably a little easier to read.
> 
>  - todd

sure

Index: process.c
===================================================================
RCS file: /cvs/src/usr.bin/sed/process.c,v
retrieving revision 1.34
diff -u -p -r1.34 process.c
--- process.c   14 Nov 2018 10:59:33 -0000      1.34
+++ process.c   10 Jan 2022 15:52:26 -0000
@@ -60,7 +60,7 @@ static SPACE HS, PS, SS;
 
 static inline int       applies(struct s_command *);
 static void             flush_appends(void);
-static void             lputs(char *);
+static void             lputs(char *, size_t);
 static inline int       regexec_e(regex_t *, const char *, int, int, size_t,
                             size_t);
 static void             regsub(SPACE *, char *, char *);
@@ -158,7 +158,7 @@ redirect:
                                (void)fprintf(outfile, "%s", cp->t);
                                break;
                        case 'l':
-                               lputs(ps);
+                               lputs(ps, psl);
                                break;
                        case 'n':
                                if (!nflag && !pd)
@@ -478,14 +478,14 @@ flush_appends(void)
 }
 
 static void
-lputs(char *s)
+lputs(char *s, size_t len)
 {
        int count;
        extern int termwidth;
        const char *escapes;
        char *p;
 
-       for (count = 0; *s; ++s) {
+       for (count = 0; len > 0; len--, s++) {
                if (count >= termwidth) {
                        (void)fprintf(outfile, "\\\n");
                        count = 0;
@@ -501,7 +501,7 @@ lputs(char *s)
                } else {
                        escapes = "\\\a\b\f\r\t\v";
                        (void)fputc('\\', outfile);
-                       if ((p = strchr(escapes, *s))) {
+                       if ((p = strchr(escapes, *s)) && *s != '\0') {
                                (void)fputc("\\abfrtv"[p - escapes], outfile);
                                count += 2;
                        } else {

Reply via email to