Hi all,

v2 below following some feedback from Christian (naddy@, thanks!). Only
change is the malloc + strlcpy is replaced with a strdup.

Thanks,
Alex


diff --git games/quiz/quiz.c games/quiz/quiz.c
index 073c1700719..f6eac5027e8 100644
--- games/quiz/quiz.c
+++ games/quiz/quiz.c
@@ -110,7 +110,8 @@ get_file(const char *file)
 {
        FILE *fp;
        QE *qp;
-       size_t len;
+       ssize_t len;
+       size_t size;
        char *lp;
 
        if ((fp = fopen(file, "r")) == NULL)
@@ -123,9 +124,11 @@ get_file(const char *file)
         */
        qp = &qlist;
        qsize = 0;
-       while ((lp = fgetln(fp, &len)) != NULL) {
+       lp = NULL;
+       size = 0;
+       while ((len = getline(&lp, &size, fp)) != -1) {
                if (lp[len - 1] == '\n')
-                       --len;
+                       lp[len - 1] = '\0';
                if (qp->q_text && qp->q_text[0] != '\0' &&
                    qp->q_text[strlen(qp->q_text) - 1] == '\\')
                        qp->q_text = appdstr(qp->q_text, lp, len);
@@ -133,16 +136,17 @@ get_file(const char *file)
                        if ((qp->q_next = malloc(sizeof(QE))) == NULL)
                                errx(1, "malloc");
                        qp = qp->q_next;
-                       if ((qp->q_text = malloc(len + 1)) == NULL)
-                               errx(1, "malloc");
-                       /* lp may not be zero-terminated; cannot use strlcpy */
-                       strncpy(qp->q_text, lp, len);
-                       qp->q_text[len] = '\0';
+                       qp->q_text = strdup(lp);
+                       if (qp->q_text == NULL)
+                               err(1, NULL);
                        qp->q_asked = qp->q_answered = FALSE;
                        qp->q_next = NULL;
                        ++qsize;
                }
        }
+       free(lp);
+       if (ferror(fp))
+               err(1, "getline");
        (void)fclose(fp);
 }
 
@@ -334,11 +338,9 @@ appdstr(char *s, const char *tp, size_t len)
        if (*(mp - 1) == '\\')
                --mp;
 
-       while ((ch = *mp++ = *tp++) && ch != '\n')
+       /* tp guaranteed null-terminated, copy in full */
+       while ((ch = *mp++ = *tp++) != '\0')
                ;
-       if (*(mp - 2) == '\\')
-               mp--;
-       *mp = '\0';
 
        free(s);
        return (m);

Reply via email to