acme-client: prevent duplicate definitions of global variables

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

acme-client: prevent duplicate definitions of global variables

Michael Forney
Every source file that includes extern.h will have its own definition
of these variables. Since many compilers allocate the variables with
.comm, they end up getting merged by the linker without error.
However, ISO C requires exactly one definition of objects with
external linkage.

gcc 10 will enable -fno-common by default, which will put
zero-initialized data in .bss, causing linking errors when multiple
definitions are present.
---
 usr.sbin/acme-client/extern.h | 4 ++--
 usr.sbin/acme-client/main.c   | 5 +++--
 2 files changed, 5 insertions(+), 4 deletions(-)

diff --git a/usr.sbin/acme-client/extern.h b/usr.sbin/acme-client/extern.h
index e6b7af0d05b..f280b3e279e 100644
--- a/usr.sbin/acme-client/extern.h
+++ b/usr.sbin/acme-client/extern.h
@@ -277,12 +277,12 @@ char *json_fmt_signed(const char *, const char *, const char *);
 /*
  * Should we print debugging messages?
  */
-int verbose;
+extern int verbose;
 
 /*
  * What component is the process within (COMP__MAX for none)?
  */
-enum comp proccomp;
+extern enum comp proccomp;
 
 __END_DECLS
 
diff --git a/usr.sbin/acme-client/main.c b/usr.sbin/acme-client/main.c
index 7cbeeb7de03..1f59e6c755d 100644
--- a/usr.sbin/acme-client/main.c
+++ b/usr.sbin/acme-client/main.c
@@ -32,6 +32,9 @@
 #define WWW_DIR "/var/www/acme"
 #define CONF_FILE "/etc/acme-client.conf"
 
+int verbose;
+enum comp proccomp;
+
 int
 main(int argc, char *argv[])
 {
@@ -46,8 +49,6 @@ main(int argc, char *argv[])
  int  c, rc, revocate = 0;
  int  popts = 0;
  pid_t  pids[COMP__MAX];
- extern int  verbose;
- extern enum comp  proccomp;
  size_t  i, altsz, ne;
 
  struct acme_conf *conf = NULL;
--
2.25.0

Reply | Threaded
Open this post in threaded view
|

Re: acme-client: prevent duplicate definitions of global variables

Jeremie Courreges-Anglas-2
On Fri, Jan 31 2020, Michael Forney <[hidden email]> wrote:
> Every source file that includes extern.h will have its own definition
> of these variables. Since many compilers allocate the variables with
> .comm, they end up getting merged by the linker without error.
> However, ISO C requires exactly one definition of objects with
> external linkage.

LGTM, ok jca@.

I'll commit it if none of the usual suspects show up soon.

> gcc 10 will enable -fno-common by default, which will put
> zero-initialized data in .bss, causing linking errors when multiple
> definitions are present.

Good to know, thanks.

> ---
>  usr.sbin/acme-client/extern.h | 4 ++--
>  usr.sbin/acme-client/main.c   | 5 +++--
>  2 files changed, 5 insertions(+), 4 deletions(-)
>
> diff --git a/usr.sbin/acme-client/extern.h b/usr.sbin/acme-client/extern.h
> index e6b7af0d05b..f280b3e279e 100644
> --- a/usr.sbin/acme-client/extern.h
> +++ b/usr.sbin/acme-client/extern.h
> @@ -277,12 +277,12 @@ char *json_fmt_signed(const char *, const char *, const char *);
>  /*
>   * Should we print debugging messages?
>   */
> -int verbose;
> +extern int verbose;
>  
>  /*
>   * What component is the process within (COMP__MAX for none)?
>   */
> -enum comp proccomp;
> +extern enum comp proccomp;
>  
>  __END_DECLS
>  
> diff --git a/usr.sbin/acme-client/main.c b/usr.sbin/acme-client/main.c
> index 7cbeeb7de03..1f59e6c755d 100644
> --- a/usr.sbin/acme-client/main.c
> +++ b/usr.sbin/acme-client/main.c
> @@ -32,6 +32,9 @@
>  #define WWW_DIR "/var/www/acme"
>  #define CONF_FILE "/etc/acme-client.conf"
>  
> +int verbose;
> +enum comp proccomp;
> +
>  int
>  main(int argc, char *argv[])
>  {
> @@ -46,8 +49,6 @@ main(int argc, char *argv[])
>   int  c, rc, revocate = 0;
>   int  popts = 0;
>   pid_t  pids[COMP__MAX];
> - extern int  verbose;
> - extern enum comp  proccomp;
>   size_t  i, altsz, ne;
>  
>   struct acme_conf *conf = NULL;

--
jca | PGP : 0x1524E7EE / 5135 92C1 AD36 5293 2BDF  DDCC 0DFA 74AE 1524 E7EE

Reply | Threaded
Open this post in threaded view
|

Re: acme-client: prevent duplicate definitions of global variables

Florian Obser-2
committed, thanks!

On Sat, Feb 01, 2020 at 01:54:11PM +0100, Jeremie Courreges-Anglas wrote:

> On Fri, Jan 31 2020, Michael Forney <[hidden email]> wrote:
> > Every source file that includes extern.h will have its own definition
> > of these variables. Since many compilers allocate the variables with
> > .comm, they end up getting merged by the linker without error.
> > However, ISO C requires exactly one definition of objects with
> > external linkage.
>
> LGTM, ok jca@.
>
> I'll commit it if none of the usual suspects show up soon.
>
> > gcc 10 will enable -fno-common by default, which will put
> > zero-initialized data in .bss, causing linking errors when multiple
> > definitions are present.
>
> Good to know, thanks.
>
> > ---
> >  usr.sbin/acme-client/extern.h | 4 ++--
> >  usr.sbin/acme-client/main.c   | 5 +++--
> >  2 files changed, 5 insertions(+), 4 deletions(-)
> >
> > diff --git a/usr.sbin/acme-client/extern.h b/usr.sbin/acme-client/extern.h
> > index e6b7af0d05b..f280b3e279e 100644
> > --- a/usr.sbin/acme-client/extern.h
> > +++ b/usr.sbin/acme-client/extern.h
> > @@ -277,12 +277,12 @@ char *json_fmt_signed(const char *, const char *, const char *);
> >  /*
> >   * Should we print debugging messages?
> >   */
> > -int verbose;
> > +extern int verbose;
> >  
> >  /*
> >   * What component is the process within (COMP__MAX for none)?
> >   */
> > -enum comp proccomp;
> > +extern enum comp proccomp;
> >  
> >  __END_DECLS
> >  
> > diff --git a/usr.sbin/acme-client/main.c b/usr.sbin/acme-client/main.c
> > index 7cbeeb7de03..1f59e6c755d 100644
> > --- a/usr.sbin/acme-client/main.c
> > +++ b/usr.sbin/acme-client/main.c
> > @@ -32,6 +32,9 @@
> >  #define WWW_DIR "/var/www/acme"
> >  #define CONF_FILE "/etc/acme-client.conf"
> >  
> > +int verbose;
> > +enum comp proccomp;
> > +
> >  int
> >  main(int argc, char *argv[])
> >  {
> > @@ -46,8 +49,6 @@ main(int argc, char *argv[])
> >   int  c, rc, revocate = 0;
> >   int  popts = 0;
> >   pid_t  pids[COMP__MAX];
> > - extern int  verbose;
> > - extern enum comp  proccomp;
> >   size_t  i, altsz, ne;
> >  
> >   struct acme_conf *conf = NULL;
>
> --
> jca | PGP : 0x1524E7EE / 5135 92C1 AD36 5293 2BDF  DDCC 0DFA 74AE 1524 E7EE
>

--
I'm not entirely sure you are real.