After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 756903 - Add some helpers for bounds-checked unsigned integer arithemtic
Add some helpers for bounds-checked unsigned integer arithemtic
Status: RESOLVED DUPLICATE of bug 503096
Product: glib
Classification: Platform
Component: general
unspecified
Other All
: Normal normal
: ---
Assigned To: gtkdev
gtkdev
Depends on:
Blocks: 756906
 
 
Reported: 2015-10-21 11:45 UTC by Allison Karlitskaya (desrt)
Modified: 2015-10-27 14:24 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
macros: add dummy __has_builtin() (952 bytes, patch)
2015-10-21 11:45 UTC, Allison Karlitskaya (desrt)
reviewed Details | Review
GLib: add bounds-checked unsigned int arithmetic (7.92 KB, patch)
2015-10-21 11:45 UTC, Allison Karlitskaya (desrt)
reviewed Details | Review
tests: test bounds-checked int arithmetic (8.64 KB, patch)
2015-10-21 11:45 UTC, Allison Karlitskaya (desrt)
reviewed Details | Review

Description Allison Karlitskaya (desrt) 2015-10-21 11:45:42 UTC
GCC5 recently gained support for compiler intrinsics to do efficient
bounds-checked arithmetic and Clang has had the same for a while.

Meanwhile, it would be nice if we could put an end to doubts about the
'best way' to write bounds-checked integer math code.

These macros should help with both of those things and will be used in
an upcoming patch to add support for a bounds-checked getter to GBytes.
Comment 1 Allison Karlitskaya (desrt) 2015-10-21 11:45:46 UTC
Created attachment 313806 [details] [review]
macros: add dummy __has_builtin()

Add a dummy definition for Clang's __has_builtin() macro.  This will
allow us to use __has_builtin() unconditionally, in the same way as we
already do for __has_feature().
Comment 2 Allison Karlitskaya (desrt) 2015-10-21 11:45:52 UTC
Created attachment 313807 [details] [review]
GLib: add bounds-checked unsigned int arithmetic

Add some helpers for builds-checked unsigned integer arithmetic to GLib.
These will be based on compiler intrinsics where they are available,
falling back to standard manual checks otherwise.

The fallback case needs to be implemented as a function (which we do
inline) because we cannot rely on statement expressions.  We also
implement the intrinsics case as an inline in order to avoid people
accidentally writing non-portable code which depends on static
evaluation of the builtin.

For now there is only support for addition and multiplication for guint,
guint64 and gsize.  It may make sense to add support for subtraction or
for the signed equivalents of those types in the future if we find a use
for that.
Comment 3 Allison Karlitskaya (desrt) 2015-10-21 11:45:59 UTC
Created attachment 313808 [details] [review]
tests: test bounds-checked int arithmetic

Add some simple testcases for the new bounds-checked integer arithmetic
helpers.

Include a second build of the testcase to make sure we test the fallback
code even if we are on a compiler that supports the intrinsics.
Comment 4 Emmanuele Bassi (:ebassi) 2015-10-21 12:45:09 UTC
See also: bug 503096, which adds a bunch of inline functions with fallbacks.
Comment 5 Emmanuele Bassi (:ebassi) 2015-10-21 12:45:49 UTC
To be fair, I kind of like the approach in bug 503096 the most, even if it adds a metric ton of symbols.
Comment 6 Dan Winship 2015-10-21 22:29:36 UTC
(In reply to Emmanuele Bassi (:ebassi) from comment #5)
> To be fair, I kind of like the approach in bug 503096 the most

Which part of it exactly? Looking function-y rather than macro-y? Supporting both int and uint? Having int and long versions in addition to int32 and int64 versions? Having clamped versions in addition to the give-the-wrong-answer-and-return-false versions? Having subtraction support?

(I'd say "don't care", "probably want", "probably don't want", "don't know", and "don't know", respectively.)
Comment 7 Philip Withnall 2015-10-22 11:24:43 UTC
Review of attachment 313806 [details] [review]:

This seems useful in general.
Comment 8 Philip Withnall 2015-10-22 11:29:21 UTC
Review of attachment 313807 [details] [review]:

Pending working out whether to take the approach in this bug or in bug #503096, this patch looks good to me.
Comment 9 Philip Withnall 2015-10-22 11:30:50 UTC
Review of attachment 313808 [details] [review]:

Also looks fine to me.
Comment 10 Emmanuele Bassi (:ebassi) 2015-10-22 11:42:40 UTC
(In reply to Dan Winship from comment #6)
> (In reply to Emmanuele Bassi (:ebassi) from comment #5)
> > To be fair, I kind of like the approach in bug 503096 the most
> 
> Which part of it exactly? Looking function-y rather than macro-y? Supporting
> both int and uint? Having int and long versions in addition to int32 and
> int64 versions? Having clamped versions in addition to the
> give-the-wrong-answer-and-return-false versions? Having subtraction support?

All of them. :-)

 - Type safe inlined functions instead of macros: ✓ (I want the compiler to kick me if I'm doing something stupid)
 - Supporting signed and unsigned integers: ✓
 - Supporting more types: ✓ (though this is the one thing I can live without, as long as we have support for sized integers)
 - Safe clamped versions: ✓ (saves a branch to handle failure)
 - More arithmetic operators: ✓ (I'll admit that overflowing addition is rarer, but still very much possible in existing code, see bug 755514)
Comment 11 Dan Winship 2015-10-22 13:21:06 UTC
(In reply to Emmanuele Bassi (:ebassi) from comment #10)
>  - Safe clamped versions: ✓ (saves a branch to handle failure)

What is the use case? In the GBytes example, overflow is an error, and we want to return NULL in that case. And clamping seems wrong in bug 755514 too; if x*y>MAXUINT, then you just can't represent that object, and you should bail out, not allocate a buffer that isn't actually large enough. Right?
Comment 12 Allison Karlitskaya (desrt) 2015-10-27 14:24:03 UTC
Let's unify the discussion on this topic.  It's my fault that I opened this without checking existing reports.

*** This bug has been marked as a duplicate of bug 503096 ***