Re: [PATCH] support ptrdiff_t if it has the same storage size as int, but does not alias it
"'Robin Haberkorn' via scintilla-interest" <[email protected]> Sun, 26 Oct 2025 14:00:26 +0000
| Newsgroups | gmane.comp.lib.scintilla.devel |
|---|---|
| Message-ID | <[email protected]> |
On Thu Oct 23, 2025 at 23:16:24 GMT +02, Neil Hodgson wrote: > The standards allow lots of weird choices like defining NULL to not > be all 0 bits. Platforms that behave differently to the big 3 get to > live with the consequences of their choices. Fair point - practically nobody thinks about that. E.g. memset(0) will break on those platforms if your struct contains any pointers and you expect them to end up with NULL pointers afterwards. However, are there *any* actual systems with standard C/C++ compilers with non-zero NULL pointers? I don't think so. And arguably aliasing rules of ptrdiff_t* are much less obvious. > 32-bit systems are becoming quite rare. On macOS, Scintilla went > 64-bit only in 2014 and support effort would be decreased by only > supporting 64-bit platforms. Does anybody guarantee that int will continue to be 32-bit forever? Perhaps there will once be a platform with 128-bit ints and ptrdiff_t or something. > Compile-time options should be listed in the table in the "Building > Scintilla" section at the end of doc/ScintillaDoc.html. Here's yet another version of the patch, updating ScintillaDoc.html. Robin -- You received this message because you are subscribed to the Google Groups "scintilla-interest" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion visit https://groups.google.com/d/msgid/scintilla-interest/DDSB3WDYI90Z.38QVTKDH2HYL3%40googlemail.com.
v4-0001-support-ptrdiff_t-if-it-has-the-same-storage-size.patch
(text/x-patch, 3.6 KB)
From db63b6316586873ae1fd13318787dca586026645 Mon Sep 17 00:00:00 2001 From: Robin Haberkorn <[email protected]> Date: Sun, 19 Oct 2025 23:40:08 +0200 Subject: [PATCH v4] support ptrdiff_t if it has the same storage size as int, but does *not* alias it * This is the case e.g. on NetBSD 10 for ARMv6 where Sci::Position == ptrdiff_t == long int, but obviously for other platforms as well, where it causes "invalid conversion" and "undefined symbol" errors. Scintilla was testing for aliasability by comparing the storage size with sizeof() or PTRDIFF_MAX == INT_MAX at the preprocessor level. This was fundamentally flawed. * In LineVector<T>::InsertLines() we are now using the C++17 construct std::is_convertible_v<From*,To*> instead. * We need RunStyles<ptrdiff_t> as well on the affected platforms. This is impossible to test for in a constant expression that can be used with the preprocessor. Also, it's not possible to conditionally instantiate templates. We tried to instantiate RunStyles for all scalar types that could be behind ptrdiff_t, but it was causing warnings on MSVC. Implicitly instantiating RunStyles would be possible, but is not desired. Therefore as a workaround, you can now define the PTRDIFF_DOESNT_ALIAS_INT macro when invoking the build system, to force instantiating RunStyles<ptrdiff_t>. When writing portable applications, you may have to use a compile-time check for checking aliasability of ptrdiff_t and int in order to define PTRDIFF_DOESNT_ALIAS_INT. --- doc/ScintillaDoc.html | 7 +++++++ src/CellBuffer.cxx | 3 ++- src/RunStyles.cxx | 2 +- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/doc/ScintillaDoc.html b/doc/ScintillaDoc.html index 5c92043cc..82829a947 100644 --- a/doc/ScintillaDoc.html +++ b/doc/ScintillaDoc.html @@ -10529,6 +10529,13 @@ EM_SETTARGETDEVICE <td align="left"><code>DISABLE_D2D</code></td> <td>(Win32) Build Scintilla without Direct2D/DirectWrite.</td> </tr> + + <tr> + <td align="left"><code>PTRDIFF_DOESNT_ALIAS_INT</code></td> + <td>Define if <code>ptrdiff_t*</code> does not alias <code>int*</code>. + This is detected automatically, but the check may be unreliable. + Try to define this symbol in case of errors about undefined symbols.</td> + </tr> </tbody> </table> diff --git a/src/CellBuffer.cxx b/src/CellBuffer.cxx index 04486d4c6..3e9deb934 100644 --- a/src/CellBuffer.cxx +++ b/src/CellBuffer.cxx @@ -21,6 +21,7 @@ #include <optional> #include <algorithm> #include <memory> +#include <type_traits> #include "ScintillaTypes.h" @@ -215,7 +216,7 @@ public: } void InsertLines(Sci::Line line, const Sci::Position *positions, size_t lines, bool lineStart) override { const POS lineAsPos = pos_cast(line); - if constexpr (sizeof(Sci::Position) == sizeof(POS)) { + if constexpr (std::is_convertible_v<Sci::Position *, POS *>) { starts.InsertPartitions(lineAsPos, positions, lines); } else { starts.InsertPartitionsWithCast(lineAsPos, positions, lines); diff --git a/src/RunStyles.cxx b/src/RunStyles.cxx index 848670ba9..bca45c889 100644 --- a/src/RunStyles.cxx +++ b/src/RunStyles.cxx @@ -321,7 +321,7 @@ void RunStyles<DISTANCE, STYLE>::Check() const { template class Scintilla::Internal::RunStyles<int, int>; template class Scintilla::Internal::RunStyles<int, char>; -#if (PTRDIFF_MAX != INT_MAX) || defined(__HAIKU__) +#if (PTRDIFF_MAX != INT_MAX) || defined(__HAIKU__) || defined(PTRDIFF_DOESNT_ALIAS_INT) template class Scintilla::Internal::RunStyles<ptrdiff_t, int>; template class Scintilla::Internal::RunStyles<ptrdiff_t, char>; #endif -- 2.50.1
signature.asc
(application/pgp-signature, 488 B)
-----BEGIN PGP SIGNATURE----- iQEzBAABCAAdFiEEB/6AD3js1eGB4raKarMDYY4q6voFAmj+KXsACgkQarMDYY4q 6vqfPQf/bb1C03tmFuzP6pAiW6Jqm9xB+wUEhVOjqnl5egCdzziNr+/N/7vud1ac ck9VguRzppYXELj89bcf4Zt6sBFR7WsI28h0IIV80atU0SNueTg7xw/bvj/aOnTM oF1v3VPQ95xJgkY7H4uDMWv0yMQuzbIYV9I1MTH175zmXNLEc+pWVOdJydyry6Wx qejoOB1o8MXRwwnJ7+DzX9LUhYQ5U8vypCzijRRZpIuROKyc/1aMmT3FOdeVQfQU 5hpn8Yb/zgVZccl8hI82Q6v8xY1KY/Vyhm005QdVkxBZI6XlVyxXZz2Y/1wleg46 bJRqWIgMpO7UGsCzLsyAhDGiYR8ZRA== =2cT1 -----END PGP SIGNATURE-----