delorie.com/archives/browse.cgi   search  
Mail Archives: geda-user/2023/02/02/11:00:18

X-Authentication-Warning: delorie.com: mail set sender to geda-user-bounces using -f
X-Recipient: geda-user AT delorie DOT com
X-Mailer: exmh version 2.8.0 04/21/2012 with nmh-1.7+dev
X-Exmh-Isig-CompType: repl
X-Exmh-Isig-Folder: inbox
From: "karl AT aspodata DOT se [via geda-user AT delorie DOT com]" <geda-user AT delorie DOT com>
To: geda-user AT delorie DOT com
Subject: Re: [geda-user] strncpy in pcb
In-reply-to: <xna61x2sf8.fsf@envy.delorie.com>
References: <xna61x2sf8 DOT fsf AT envy DOT delorie DOT com>
Comments: In-reply-to DJ Delorie <dj AT delorie DOT com>
message dated "Wed, 01 Feb 2023 12:42:51 -0500."
Mime-Version: 1.0
Message-Id: <20230202154018.8BD4085E50B5@turkos.aspodata.se>
Date: Thu, 2 Feb 2023 16:40:18 +0100 (CET)
X-Virus-Scanned: ClamAV using ClamSMTP
Reply-To: geda-user AT delorie DOT com
Errors-To: nobody AT delorie DOT com
X-Mailing-List: geda-user AT delorie DOT com
X-Unsubscribes-To: listserv AT delorie DOT com

DJ Delorie:
> "karl AT aspodata DOT se [via geda-user AT delorie DOT com]" <geda-user AT delorie DOT com>
> writes:
..
> I was planning on #defining strncpy to something else, which would hit
> all of them at once.  That something else could fault in some
> pcb-specific way.

That seems suboptimal.

 It is found in 11 files:
$ find . -type f -name \*.c -print0  | xargs -0 grep '\bstrncpy' | cut -f1 -d: |sort -u
./geda-gaf/contrib/gmk_sym/convert_sym.c
./geda-gaf/contrib/gmk_sym/gmk_sym.c
./geda-gaf/contrib/sarlacc_schem/sarlacc_schem.c
./geda-gaf/utils/src/grenum.c
./gts/named.c
./intl/localename.c
./intl/setlocale.c
./src/file.c
./src/hid/gsvit/gsvit.c
./src/mymem.c
./src/pcb-printf.c
$ find . -type f -name \*.c -print0  | xargs -0 grep '\bstrncpy' | cut -f1 -d: |sort -u | wc 
     11      11     280
$

I think thoose in intl and src are safe:

$ grep strncpy intl/localename.c
  strncpy (lname, locale_name, sizeof (lname) - 1);
$ grep strncpy intl/setlocale.c
          strncpy (lc_messages_name, locale, sizeof (lc_messages_name) - 1);
$ grep strncpy src/file.c
      strncpy (toppath, p, sizeof (toppath) - 1);
$ grep -B3 strncpy src/hid/gsvit/gsvit.c 
    char buff[0x100 + 1];
    char* src = buff;
    t = time (NULL);
    strncpy (buff, ctime(&t), 0x100);
$ grep -C1 strncpy src/mymem.c 
      copy = (char *)realloc (NULL, length + 1);
      strncpy (copy, p1, length);
      copy[length] = '\0';
$ grep -B10 -A1 strncpy src/pcb-printf.c
int pcb_snprintf(char *string, size_t size, const char *fmt, ...)
{
  gchar *tmp;
  gsize length;

  va_list args;
  va_start(args, fmt);

  tmp = pcb_vprintf (fmt, args);
  length = strlen (tmp);
  strncpy (string, tmp, size);
  string[size - 1] = '\0';
$

Generally, it would be best to use the form:
 strncpy(dst,src,sizeof(dst)-1)
whenever possible, but I agree that strlcpy() has a better
interface and the above would be:
 strlcpy(dst,src,sizeof(dst))

> > Since pcb is using glib, perhaps using its string handling
> > functions would be friutful, or possible any other already
> > written string handling library.
> Suggestions welcome. I'm just trying to fix new warnings that happen
> with gcc 13 -std=gnu99

Ok, a new lib would req. a rewrite.

I don't have gcc 13, just v11. What is the error message ?
Does strncpy(dst,src,sizeof(dst)-1) have the same error message,
as if you compile the intl directory ?

 Gts has three strncpy which looks similar:
$ grep strncpy gts/named.c
    strncpy (GTS_NVERTEX (*po)->name, fp->token->str, GTS_NAME_LENGTH);
  strncpy (GTS_NEDGE (*po)->name, fp->token->str, GTS_NAME_LENGTH);
  strncpy (GTS_NFACE (*po)->name, fp->token->str, GTS_NAME_LENGTH);
$ fgrep -B2  'name[GTS_NAME_LENGTH]' gts/*.h
gts/gts.h-struct _GtsNVertex {
gts/gts.h-  GtsVertex parent;
gts/gts.h:  char name[GTS_NAME_LENGTH];
--
gts/gts.h-struct _GtsNEdge {
gts/gts.h-  GtsEdge parent;
gts/gts.h:  char name[GTS_NAME_LENGTH];
--
gts/gts.h-struct _GtsNFace {
gts/gts.h-  GtsFace parent;
gts/gts.h:  char name[GTS_NAME_LENGTH];
$

Would changing:
  strncpy (GTS_NEDGE (*po)->name, fp->token->str, GTS_NAME_LENGTH);
to
  char *dst = GTS_NEDGE (*po)->name;
  strncpy (dst, fp->token->str, sizeof(dst)-1);
(and simil) solve the issue ?

Regards,
/Karl Hammar


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019