bug-gnulib@gnu.org mirror (unofficial)
 help / color / mirror / Atom feed
From: Bruno Haible <bruno@clisp.org>
To: noloader@gmail.com
Cc: bug-gnulib@gnu.org
Subject: Re: RFC: add a string-desc module
Date: Sat, 25 Mar 2023 12:49:57 +0100	[thread overview]
Message-ID: <5361047.C4sosBPzcN@nimes> (raw)
In-Reply-To: <CAH8yC8mfviFog+wASgUsr7Te59WKxKF=xAeajY=K5OeMk6GRrg@mail.gmail.com>

Jeffrey Walton wrote:
> A natural thing to want
> to do is print a string, and C-based routines usually expect a
> terminating NULL.

I'll add a comment regarding printf with the "%.*s" directive.

> Also, if you initialize the struct, then the allocated string will
> likely include a terminating NULL. I understand the size member will
> omit the NULL, but it will be present anyways in the string.

No; it depends where the 'char *' comes from. If it is a pointer into
a piece of memory read through read_file, for example, there will be
no NUL terminator.

Also, in C you can write
  char buf[4] = "abcd";
which does not add a NUL.

> A length prefixed string may be a good idea.

https://github.com/antirez/sds does it like this. But again, this
does not allow for an allocation-free substring function.

> So if you are going to add the "string descriptor", then I hope you
> add some functions to make it easier for less experienced folks to
> write safer code.

I believe all these functions are already in the proposal.

> Also see libbsd's stringlist.h for some inspiration,
> https://cgit.freedesktop.org/libbsd/tree/include/bsd/stringlist.h .

This is unrelated, AFAICS. It's not about a string, but about an
extensible array of strings.

Bruno





  parent reply	other threads:[~2023-03-25 11:50 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-24 21:50 RFC: add a string-desc module Bruno Haible
2023-03-24 22:32 ` Paul Eggert
2023-03-25 11:39   ` Bruno Haible
2023-03-24 23:20 ` Jeffrey Walton
2023-03-25  6:25   ` Vivien Kraus
2023-03-25 11:49   ` Bruno Haible [this message]
2023-03-25 15:51     ` Paul Eggert
2023-03-28 22:40       ` Bruno Haible
2023-03-25  6:21 ` Vivien Kraus
2023-03-25 11:56   ` Bruno Haible
2023-03-27 10:15 ` Simon Josefsson via Gnulib discussion list
2023-03-28 22:49   ` Bruno Haible

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://lists.gnu.org/mailman/listinfo/bug-gnulib

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5361047.C4sosBPzcN@nimes \
    --to=bruno@clisp.org \
    --cc=bug-gnulib@gnu.org \
    --cc=noloader@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).