bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

immutable string type


From: Bruno Haible
Subject: immutable string type
Date: Sat, 28 Dec 2019 12:17:36 +0100
User-agent: KMail/5.1.3 (Linux/4.4.0-170-generic; KDE/5.18.0; x86_64; ; )

Hi all,

Would you find it useful to have an immutable string type in gnulib?

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.

Bruno




reply via email to

[Prev in Thread] Current Thread [Next in Thread]