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 756906 - GBytes: add range-checked pointer getter
GBytes: add range-checked pointer getter
Status: RESOLVED OBSOLETE
Product: glib
Classification: Platform
Component: general
unspecified
Other All
: Normal normal
: ---
Assigned To: gtkdev
gtkdev
Depends on: 756903
Blocks:
 
 
Reported: 2015-10-21 12:14 UTC by Allison Karlitskaya (desrt)
Modified: 2018-05-24 18:18 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
GBytes: add range-checked pointer getter (6.17 KB, patch)
2015-10-21 12:14 UTC, Allison Karlitskaya (desrt)
needs-work Details | Review

Description Allison Karlitskaya (desrt) 2015-10-21 12:14:27 UTC
Add a helper to GBytes to perform the common task of:

 - multiplying an element size times an array length, with bounds checks

 - adding it to an offset, with bounds checks

 - ensuring the resulting end offset is in range of the GBytes

 - returning a pointer to the start of the range

This is built on the new efficient checked integer arithmetic macros
introduced in bug 756903.

Also add a testcase.
Comment 1 Allison Karlitskaya (desrt) 2015-10-21 12:14:32 UTC
Created attachment 313811 [details] [review]
GBytes: add range-checked pointer getter
Comment 2 Dan Winship 2015-10-21 22:31:07 UTC
(In reply to Ryan Lortie (desrt) from comment #0)
> Add a helper to GBytes to perform the common task of:
                                              ^[citation required]
Comment 3 Philip Withnall 2015-10-22 11:17:23 UTC
(In reply to Dan Winship from comment #2)
> (In reply to Ryan Lortie (desrt) from comment #0)
> > Add a helper to GBytes to perform the common task of:
>                                               ^[citation required]

See bug #756026 for one example; bug #756100 for another. Basically anything where structured data is being loaded from an mmap()ed file.
Comment 4 Philip Withnall 2015-10-22 11:23:49 UTC
Review of attachment 313811 [details] [review]:

::: glib/gbytes.c
@@ +500,3 @@
+
+/**
+ * g_bytes_get_pointer:

The name implies to me that this does the same as g_bytes_get_data() — i.e. it returns the base point of the GBytes.

How about g_bytes_get_region()?

@@ +515,3 @@
+ *
+ * This function does careful bounds checking (including checking for
+ * arithmatic overflows) and returns a non-%NULL pointer if the

s/arithmatic/arithmetic/

@@ +520,3 @@
+ * is returned.
+ *
+ * Note: it is possible to have a valid zero-size range.  In this case,

s/range/region/, since that's what you've used elsewhere in the docs. Maybe also clarify that this is the case iff @n_elements is zero. Combine it with the paragraph which states that (paragraph 3)?

@@ +545,3 @@
+
+  if (!GSIZE_CHECKED_MUL (&total_size, element_size, n_elements))
+    return FALSE;

s/FALSE/NULL/, and below.
Comment 5 Allison Karlitskaya (desrt) 2015-10-27 14:45:08 UTC
I will redo the patch once we come to a conclusion on bug 503096.
Comment 6 GNOME Infrastructure Team 2018-05-24 18:18:52 UTC
-- GitLab Migration Automatic Message --

This bug has been migrated to GNOME's GitLab instance and has been closed from further activity.

You can subscribe and participate further through the new bug through this link to our GitLab instance: https://gitlab.gnome.org/GNOME/glib/issues/1098.