fix rpki-client regress

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

fix rpki-client regress

Claudio Jeker
Refactor the TAL loading code a bit (move the file parsing back into
tal.c) and adjust the regress test to use this new function. This fixes
the regress test and makes the code a bit nicer.

OK?
--
:wq Claudio

Index: usr.sbin/rpki-client/extern.h
===================================================================
RCS file: /cvs/src/usr.sbin/rpki-client/extern.h,v
retrieving revision 1.10
diff -u -p -r1.10 extern.h
--- usr.sbin/rpki-client/extern.h 31 Oct 2019 08:36:43 -0000 1.10
+++ usr.sbin/rpki-client/extern.h 1 Nov 2019 19:59:50 -0000
@@ -230,6 +230,7 @@ extern int verbose;
 void tal_buffer(char **, size_t *, size_t *, const struct tal *);
 void tal_free(struct tal *);
 struct tal *tal_parse(const char *, char *);
+char *tal_read_file(const char *);
 struct tal *tal_read(int);
 
 void cert_buffer(char **, size_t *, size_t *, const struct cert *);
Index: usr.sbin/rpki-client/main.c
===================================================================
RCS file: /cvs/src/usr.sbin/rpki-client/main.c,v
retrieving revision 1.21
diff -u -p -r1.21 main.c
--- usr.sbin/rpki-client/main.c 31 Oct 2019 08:36:43 -0000 1.21
+++ usr.sbin/rpki-client/main.c 1 Nov 2019 20:01:35 -0000
@@ -22,7 +22,6 @@
 #include <sys/wait.h>
 
 #include <assert.h>
-#include <ctype.h>
 #include <err.h>
 #include <dirent.h>
 #include <fcntl.h>
@@ -463,59 +462,11 @@ queue_add_from_mft_set(int fd, struct en
 static void
 queue_add_tal(int fd, struct entityq *q, const char *file, size_t *eid)
 {
- char *nfile, *nbuf, *line = NULL, *buf = NULL;
- FILE *in;
- ssize_t n, i;
- size_t sz = 0, bsz = 0;
- int optcomment = 1;
-
- if ((in = fopen(file, "r")) == NULL)
- err(EXIT_FAILURE, "fopen: %s", file);
-
- while ((n = getline(&line, &sz, in)) != -1) {
- /* replace CRLF with just LF */
- if (n > 1 && line[n - 1] == '\n' && line[n - 2] == '\r') {
- line[n - 2] = '\n';
- line[n - 1] = '\0';
- n--;
- }
- if (optcomment) {
- /* if this is comment, just eat the line */
- if (line[0] == '#')
- continue;
- optcomment = 0;
- /*
- * Empty line is end of section and needs
- * to be eaten as well.
- */
- if (line[0] == '\n')
- continue;
- }
-
- /* make sure every line is valid ascii */
- for (i = 0; i < n; i++)
- if (!isprint(line[i]) && !isspace(line[i]))
- errx(EXIT_FAILURE, "getline: %s: "
-    "invalid content", file);
-
- /* concat line to buf */
- if ((nbuf = realloc(buf, bsz + n + 1)) == NULL)
- err(EXIT_FAILURE, NULL);
- buf = nbuf;
- bsz += n + 1;
- strlcat(buf, line, bsz);
- /* limit the buffer size */
- if (bsz > 4096)
- errx(EXIT_FAILURE, "%s: file too big", file);
- }
-
- free(line);
- if (ferror(in))
- err(EXIT_FAILURE, "getline: %s", file);
- fclose(in);
+ char *nfile, *buf;
 
  if ((nfile = strdup(file)) == NULL)
  err(EXIT_FAILURE, "strdup");
+ buf = tal_read_file(file);
 
  /* Not in a repository, so directly add to queue. */
  entityq_add(fd, q, nfile, RTYPE_TAL, NULL, NULL, NULL, 0, buf, eid);
Index: usr.sbin/rpki-client/tal.c
===================================================================
RCS file: /cvs/src/usr.sbin/rpki-client/tal.c,v
retrieving revision 1.8
diff -u -p -r1.8 tal.c
--- usr.sbin/rpki-client/tal.c 31 Oct 2019 08:36:43 -0000 1.8
+++ usr.sbin/rpki-client/tal.c 1 Nov 2019 20:01:27 -0000
@@ -17,6 +17,7 @@
 
 #include <netinet/in.h>
 #include <assert.h>
+#include <ctype.h>
 #include <err.h>
 #include <libgen.h>
 #include <resolv.h>
@@ -154,6 +155,63 @@ tal_parse(const char *fn, char *buf)
  p->descr[dlen] = 0;
 
  return p;
+}
+
+char *
+tal_read_file(const char *file)
+{
+ char *nbuf, *line = NULL, *buf = NULL;
+ FILE *in;
+ ssize_t n, i;
+ size_t sz = 0, bsz = 0;
+ int optcomment = 1;
+
+ if ((in = fopen(file, "r")) == NULL)
+ err(EXIT_FAILURE, "fopen: %s", file);
+
+ while ((n = getline(&line, &sz, in)) != -1) {
+ /* replace CRLF with just LF */
+ if (n > 1 && line[n - 1] == '\n' && line[n - 2] == '\r') {
+ line[n - 2] = '\n';
+ line[n - 1] = '\0';
+ n--;
+ }
+ if (optcomment) {
+ /* if this is comment, just eat the line */
+ if (line[0] == '#')
+ continue;
+ optcomment = 0;
+ /*
+ * Empty line is end of section and needs
+ * to be eaten as well.
+ */
+ if (line[0] == '\n')
+ continue;
+ }
+
+ /* make sure every line is valid ascii */
+ for (i = 0; i < n; i++)
+ if (!isprint(line[i]) && !isspace(line[i]))
+ errx(EXIT_FAILURE, "getline: %s: "
+    "invalid content", file);
+
+ /* concat line to buf */
+ if ((nbuf = realloc(buf, bsz + n + 1)) == NULL)
+ err(EXIT_FAILURE, NULL);
+ buf = nbuf;
+ bsz += n + 1;
+ strlcat(buf, line, bsz);
+ /* limit the buffer size */
+ if (bsz > 4096)
+ errx(EXIT_FAILURE, "%s: file too big", file);
+ }
+
+ free(line);
+ if (ferror(in))
+ err(EXIT_FAILURE, "getline: %s", file);
+ fclose(in);
+
+ return buf;
 }
 
 /*
Index: regress/usr.sbin/rpki-client//test-tal.c
===================================================================
RCS file: /cvs/src/regress/usr.sbin/rpki-client/test-tal.c,v
retrieving revision 1.3
diff -u -p -r1.3 test-tal.c
--- regress/usr.sbin/rpki-client//test-tal.c 22 Aug 2019 21:31:48 -0000 1.3
+++ regress/usr.sbin/rpki-client//test-tal.c 1 Nov 2019 20:03:18 -0000
@@ -46,6 +46,7 @@ int
 main(int argc, char *argv[])
 {
  int c, i, verb = 0;
+ char *buf;
  struct tal *tal;
 
  ERR_load_crypto_strings();
@@ -68,8 +69,10 @@ main(int argc, char *argv[])
  errx(1, "argument missing");
 
  for (i = 0; i < argc; i++) {
- if ((tal = tal_parse(argv[i])) == NULL)
+ buf = tal_read_file(argv[i]);
+ if ((tal = tal_parse(argv[i], buf)) == NULL)
  break;
+ free(buf);
  if (verb)
  tal_print(tal);
  tal_free(tal);

Reply | Threaded
Open this post in threaded view
|

Re: fix rpki-client regress

Hiltjo Posthuma
On Sat, Nov 02, 2019 at 02:24:26PM +0100, Claudio Jeker wrote:

> Refactor the TAL loading code a bit (move the file parsing back into
> tal.c) and adjust the regress test to use this new function. This fixes
> the regress test and makes the code a bit nicer.
>
> OK?
> --
> :wq Claudio
>
> Index: usr.sbin/rpki-client/extern.h
> ===================================================================
> RCS file: /cvs/src/usr.sbin/rpki-client/extern.h,v
> retrieving revision 1.10
> diff -u -p -r1.10 extern.h
> --- usr.sbin/rpki-client/extern.h 31 Oct 2019 08:36:43 -0000 1.10
> +++ usr.sbin/rpki-client/extern.h 1 Nov 2019 19:59:50 -0000
> @@ -230,6 +230,7 @@ extern int verbose;
>  void tal_buffer(char **, size_t *, size_t *, const struct tal *);
>  void tal_free(struct tal *);
>  struct tal *tal_parse(const char *, char *);
> +char *tal_read_file(const char *);
>  struct tal *tal_read(int);
>  
>  void cert_buffer(char **, size_t *, size_t *, const struct cert *);
> Index: usr.sbin/rpki-client/main.c
> ===================================================================
> RCS file: /cvs/src/usr.sbin/rpki-client/main.c,v
> retrieving revision 1.21
> diff -u -p -r1.21 main.c
> --- usr.sbin/rpki-client/main.c 31 Oct 2019 08:36:43 -0000 1.21
> +++ usr.sbin/rpki-client/main.c 1 Nov 2019 20:01:35 -0000
> @@ -22,7 +22,6 @@
>  #include <sys/wait.h>
>  
>  #include <assert.h>
> -#include <ctype.h>
>  #include <err.h>
>  #include <dirent.h>
>  #include <fcntl.h>
> @@ -463,59 +462,11 @@ queue_add_from_mft_set(int fd, struct en
>  static void
>  queue_add_tal(int fd, struct entityq *q, const char *file, size_t *eid)
>  {
> - char *nfile, *nbuf, *line = NULL, *buf = NULL;
> - FILE *in;
> - ssize_t n, i;
> - size_t sz = 0, bsz = 0;
> - int optcomment = 1;
> -
> - if ((in = fopen(file, "r")) == NULL)
> - err(EXIT_FAILURE, "fopen: %s", file);
> -
> - while ((n = getline(&line, &sz, in)) != -1) {
> - /* replace CRLF with just LF */
> - if (n > 1 && line[n - 1] == '\n' && line[n - 2] == '\r') {
> - line[n - 2] = '\n';
> - line[n - 1] = '\0';
> - n--;
> - }
> - if (optcomment) {
> - /* if this is comment, just eat the line */
> - if (line[0] == '#')
> - continue;
> - optcomment = 0;
> - /*
> - * Empty line is end of section and needs
> - * to be eaten as well.
> - */
> - if (line[0] == '\n')
> - continue;
> - }
> -
> - /* make sure every line is valid ascii */
> - for (i = 0; i < n; i++)
> - if (!isprint(line[i]) && !isspace(line[i]))
> - errx(EXIT_FAILURE, "getline: %s: "
> -    "invalid content", file);
> -
> - /* concat line to buf */
> - if ((nbuf = realloc(buf, bsz + n + 1)) == NULL)
> - err(EXIT_FAILURE, NULL);
> - buf = nbuf;
> - bsz += n + 1;
> - strlcat(buf, line, bsz);
> - /* limit the buffer size */
> - if (bsz > 4096)
> - errx(EXIT_FAILURE, "%s: file too big", file);
> - }
> -
> - free(line);
> - if (ferror(in))
> - err(EXIT_FAILURE, "getline: %s", file);
> - fclose(in);
> + char *nfile, *buf;
>  
>   if ((nfile = strdup(file)) == NULL)
>   err(EXIT_FAILURE, "strdup");
> + buf = tal_read_file(file);
>  
>   /* Not in a repository, so directly add to queue. */
>   entityq_add(fd, q, nfile, RTYPE_TAL, NULL, NULL, NULL, 0, buf, eid);
> Index: usr.sbin/rpki-client/tal.c
> ===================================================================
> RCS file: /cvs/src/usr.sbin/rpki-client/tal.c,v
> retrieving revision 1.8
> diff -u -p -r1.8 tal.c
> --- usr.sbin/rpki-client/tal.c 31 Oct 2019 08:36:43 -0000 1.8
> +++ usr.sbin/rpki-client/tal.c 1 Nov 2019 20:01:27 -0000
> @@ -17,6 +17,7 @@
>  
>  #include <netinet/in.h>
>  #include <assert.h>
> +#include <ctype.h>
>  #include <err.h>
>  #include <libgen.h>
>  #include <resolv.h>
> @@ -154,6 +155,63 @@ tal_parse(const char *fn, char *buf)
>   p->descr[dlen] = 0;
>  
>   return p;
> +}
> +
> +char *
> +tal_read_file(const char *file)
> +{
> + char *nbuf, *line = NULL, *buf = NULL;
> + FILE *in;
> + ssize_t n, i;
> + size_t sz = 0, bsz = 0;
> + int optcomment = 1;
> +
> + if ((in = fopen(file, "r")) == NULL)
> + err(EXIT_FAILURE, "fopen: %s", file);
> +
> + while ((n = getline(&line, &sz, in)) != -1) {
> + /* replace CRLF with just LF */
> + if (n > 1 && line[n - 1] == '\n' && line[n - 2] == '\r') {
> + line[n - 2] = '\n';
> + line[n - 1] = '\0';
> + n--;
> + }
> + if (optcomment) {
> + /* if this is comment, just eat the line */
> + if (line[0] == '#')
> + continue;
> + optcomment = 0;
> + /*
> + * Empty line is end of section and needs
> + * to be eaten as well.
> + */
> + if (line[0] == '\n')
> + continue;
> + }
> +
> + /* make sure every line is valid ascii */
> + for (i = 0; i < n; i++)
> + if (!isprint(line[i]) && !isspace(line[i]))
> + errx(EXIT_FAILURE, "getline: %s: "
> +    "invalid content", file);
> +
> + /* concat line to buf */
> + if ((nbuf = realloc(buf, bsz + n + 1)) == NULL)
> + err(EXIT_FAILURE, NULL);
> + buf = nbuf;
> + bsz += n + 1;
> + strlcat(buf, line, bsz);
> + /* limit the buffer size */
> + if (bsz > 4096)
> + errx(EXIT_FAILURE, "%s: file too big", file);
> + }
> +
> + free(line);
> + if (ferror(in))
> + err(EXIT_FAILURE, "getline: %s", file);
> + fclose(in);
> +
> + return buf;
>  }
>  
>  /*
> Index: regress/usr.sbin/rpki-client//test-tal.c
> ===================================================================
> RCS file: /cvs/src/regress/usr.sbin/rpki-client/test-tal.c,v
> retrieving revision 1.3
> diff -u -p -r1.3 test-tal.c
> --- regress/usr.sbin/rpki-client//test-tal.c 22 Aug 2019 21:31:48 -0000 1.3
> +++ regress/usr.sbin/rpki-client//test-tal.c 1 Nov 2019 20:03:18 -0000
> @@ -46,6 +46,7 @@ int
>  main(int argc, char *argv[])
>  {
>   int c, i, verb = 0;
> + char *buf;
>   struct tal *tal;
>  
>   ERR_load_crypto_strings();
> @@ -68,8 +69,10 @@ main(int argc, char *argv[])
>   errx(1, "argument missing");
>  
>   for (i = 0; i < argc; i++) {
> - if ((tal = tal_parse(argv[i])) == NULL)
> + buf = tal_read_file(argv[i]);
> + if ((tal = tal_parse(argv[i], buf)) == NULL)
>   break;
> + free(buf);
>   if (verb)
>   tal_print(tal);
>   tal_free(tal);
>

Hi,

I think this line maybe needs a cast:

        if (!isprint(line[i]) && !isspace(line[i]))

to:

        if (!isprint((unsigned char)line[i]) && !isspace((unsigned char)line[i]))


isprint(3) says:

"CAVEATS
     The argument c must be EOF or representable as an unsigned char;
     otherwise, the result is undefined."

--
Kind regards,
Hiltjo