The following diff constifies the strings in regerror.c and also makes
use of the strlcpy() return value to avoid a redundant strlen() call.

Index: regerror.c
===================================================================
RCS file: /OpenBSD/src/lib/libc/regex/regerror.c,v
retrieving revision 1.14
diff -u -p -r1.14 regerror.c
--- regerror.c  1 Nov 2015 03:45:29 -0000       1.14
+++ regerror.c  29 Dec 2020 10:24:18 -0000
@@ -44,12 +44,12 @@
 
 #include "utils.h"
 
-static char *regatoi(const regex_t *, char *, int);
+static const char *regatoi(const regex_t *, char *, int);
 
-static struct rerr {
+static const struct rerr {
        int code;
-       char *name;
-       char *explain;
+       const char *name;
+       const char *explain;
 } rerrs[] = {
        { REG_NOMATCH,  "REG_NOMATCH",  "regexec() failed to match" },
        { REG_BADPAT,   "REG_BADPAT",   "invalid regular expression" },
@@ -77,10 +77,10 @@ static struct rerr {
 size_t
 regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size)
 {
-       struct rerr *r;
+       const struct rerr *r;
        size_t len;
        int target = errcode &~ REG_ITOA;
-       char *s;
+       const char *s;
        char convbuf[50];
 
        if (errcode == REG_ATOI)
@@ -102,21 +102,21 @@ regerror(int errcode, const regex_t *pre
                        s = r->explain;
        }
 
-       len = strlen(s) + 1;
-       if (errbuf_size > 0) {
-               strlcpy(errbuf, s, errbuf_size);
-       }
+       if (errbuf_size != 0)
+               len = strlcpy(errbuf, s, errbuf_size);
+       else
+               len = strlen(s);
 
-       return(len);
+       return len + 1;
 }
 
 /*
  - regatoi - internal routine to implement REG_ATOI
  */
-static char *
+static const char *
 regatoi(const regex_t *preg, char *localbuf, int localbufsize)
 {
-       struct rerr *r;
+       const struct rerr *r;
 
        for (r = rerrs; r->code != 0; r++)
                if (strcmp(r->name, preg->re_endp) == 0)

Reply via email to