From: "Tim Rühsen" <tim.ruehsen@gmx.de>
To: Bruno Haible <bruno@clisp.org>, bug-gnulib@gnu.org
Cc: Darshit Shah <darnir@gnu.org>
Subject: Re: immutable string type
Date: Sat, 28 Dec 2019 14:35:18 +0100 [thread overview]
Message-ID: <569e571e-4d48-821c-1594-3eb30c84106a@gmx.de> (raw)
In-Reply-To: <2299371.vStytN2T3e@omega>
[-- Attachment #1.1: Type: text/plain, Size: 1821 bytes --]
Hi Bruno,
On 28.12.19 12:17, Bruno Haible wrote:
> Would you find it useful to have an immutable string type in gnulib?
The idea is good in fact had similar thoughts/needs a while ago. IMO,
the use cases are mostly in the testing area (especially fuzzing).
As a more general approach, a function that switches already allocated
memory into read-only memory would be handy. Like in
- m = malloc()
- initialize m with some data
- if in debug mode: call memmap_readonly(m) - from this point on 'm' is
read-only and a write leads to a segmentation fault.
- ...
- free(m)
Maybe it would best be integrated into glibc ?
Functions like iasprintf could then be built around existing functions
as needed, e.g. as static inline or as macro.
> In the simplest case, this would a 'const char *' where the 'const' is
> actually checked by the hardware. You allocate it through
>
> const char *str = iasprintf (...);
>
> You use it like any 'const char *'.
>
> You free it through
>
> ifree (str);
>
> not free (str). And when you attempt to write into it:
>
> ((char *) str)[0] = 'x';
>
> it crashes.
>
> The benefits I imagine:
> - no worry about security flaws through multithreaded accesses,
> - in large applications: verification that no part of the application
> is doing side effects that it shouldn't.
>
> The implementation uses mmap() to create a read-only and a read-write
> view of the same memory area. The contents of the string is filled through
> the read-write view. All other operations are done through the read-only
> view, because the address os the string is the one of the read-only view.
>
> This won't work on all platforms, e.g. HP-UX. But it will work on glibc
> systems, BSD, and Solaris, at least.
Regards, Tim
[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
next prev parent reply other threads:[~2019-12-28 13:35 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-12-28 11:17 immutable string type Bruno Haible
2019-12-28 13:35 ` Tim Rühsen [this message]
2019-12-29 9:45 ` Bruno Haible
2019-12-29 12:07 ` Tim Rühsen
2019-12-29 18:49 ` Paul Eggert
2019-12-29 19:03 ` Tim Rühsen
2019-12-30 3:11 ` Paul Eggert
2021-01-18 8:17 ` Bruno Haible
2019-12-28 16:54 ` Ben Pfaff
2019-12-29 9:29 ` Bruno Haible
2019-12-28 18:15 ` Paul Eggert
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=569e571e-4d48-821c-1594-3eb30c84106a@gmx.de \
--to=tim.ruehsen@gmx.de \
--cc=bruno@clisp.org \
--cc=bug-gnulib@gnu.org \
--cc=darnir@gnu.org \
/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).