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 608196 - Overflow-safe g_new family
Overflow-safe g_new family
Status: RESOLVED FIXED
Product: glib
Classification: Platform
Component: general
unspecified
Other Linux
: Normal normal
: ---
Assigned To: Behdad Esfahbod
gtkdev
Depends on:
Blocks:
 
 
Reported: 2010-01-26 20:49 UTC by Behdad Esfahbod
Modified: 2010-03-08 05:51 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
Patch (7.57 KB, patch)
2010-01-26 22:45 UTC, Behdad Esfahbod
none Details | Review
Updated patch (8.57 KB, patch)
2010-02-02 21:44 UTC, Behdad Esfahbod
none Details | Review
Complete patch (18.77 KB, patch)
2010-02-24 05:19 UTC, Behdad Esfahbod
accepted-commit_now Details | Review

Description Behdad Esfahbod 2010-01-26 20:49:58 UTC
Patch coming.
Comment 1 Behdad Esfahbod 2010-01-26 22:45:28 UTC
Created attachment 152361 [details] [review]
Patch
Comment 2 Dan Winship 2010-01-26 22:59:29 UTC
Comment on attachment 152361 [details] [review]
Patch

>+	   register gsize __n = (gsize) (n_blocks);		\
      ^^^^^^^^
you are a silly person
Comment 3 Behdad Esfahbod 2010-01-26 23:07:13 UTC
(In reply to comment #2)
> (From update of attachment 152361 [details] [review])
> >+	   register gsize __n = (gsize) (n_blocks);		\
>       ^^^^^^^^
> you are a silly person

Blame copy/paste.  Oh, and with that measure, glibc maintainers are the silliest ever.
Comment 4 Matthias Clasen 2010-01-27 15:38:09 UTC
Looks fine to me in general. Of course, the register should go, and it needs documentation for the new api, and ideally a test case to verify that this works as intended.
Comment 5 Behdad Esfahbod 2010-02-02 21:44:19 UTC
Created attachment 152881 [details] [review]
Updated patch

Moving the macro to g_malloc_n family instead of g_new.  Please comment.
Comment 6 Matthias Clasen 2010-02-03 01:28:37 UTC
Looks fine to me in general. Of course, the register should go, and it needs
documentation for the new api, and ideally a test case to verify that this
works as intended.
Comment 7 Behdad Esfahbod 2010-02-24 04:28:15 UTC
Ok, lets try to get this in.  We should request API freeze break request.  I work on a complete patch.
Comment 8 Behdad Esfahbod 2010-02-24 05:19:37 UTC
Created attachment 154567 [details] [review]
Complete patch

Ok, this one should be ready to go in.
Comment 9 Matthias Clasen 2010-03-03 15:56:00 UTC
Looks good to me too.

+#define CHECK_PASS(P)	p = (P); g_assert (p == NULL);
+#define CHECK_FAIL(P)	p = (P); g_assert (p != NULL);

might be more obvious to call these CHECK_NULL and CHECK_NONNULL, maybe
Comment 10 Behdad Esfahbod 2010-03-03 16:15:38 UTC
(In reply to comment #9)
> Looks good to me too.
> 
> +#define CHECK_PASS(P)    p = (P); g_assert (p == NULL);
> +#define CHECK_FAIL(P)    p = (P); g_assert (p != NULL);
> 
> might be more obvious to call these CHECK_NULL and CHECK_NONNULL, maybe

Initially I did, but then for the forking ones that name didn't work.  So I opted for these instead.  I'm writing freeze break request and will commit after.
Comment 11 Behdad Esfahbod 2010-03-03 22:55:53 UTC
Committed.  Note that apps need to be rebuilt against new glib to get the protection.
Comment 12 Jesse Zhang 2010-03-04 04:51:12 UTC
Hi,

I'm getting a bunch of redefinition errors after this commit (343cbf25c7104f782b9d0070cb623c7605dab646),

In file included from gmem.c:1174:
galiasdef.c:1756: error: redefinition of 'g_malloc_n'
gmem.c:239: error: previous definition of 'g_malloc_n' was here
galiasdef.c:1759: error: redefinition of 'g_malloc0_n'
gmem.c:256: error: previous definition of 'g_malloc0_n' was here
galiasdef.c:1774: error: redefinition of 'g_realloc_n'
gmem.c:274: error: previous definition of 'g_realloc_n' was here
galiasdef.c:1783: error: redefinition of 'g_try_malloc_n'
gmem.c:291: error: previous definition of 'g_try_malloc_n' was here
galiasdef.c:1786: error: redefinition of 'g_try_malloc0_n'
gmem.c:302: error: previous definition of 'g_try_malloc0_n' was here
galiasdef.c:1792: error: redefinition of 'g_try_realloc_n'
gmem.c:314: error: previous definition of 'g_try_realloc_n' was here
Comment 13 Christian Dywan 2010-03-04 10:06:02 UTC
Same here.
Comment 14 Alejandro Piñeiro Iglesias (IRC: infapi00) 2010-03-04 12:26:27 UTC
Same here, and on the build.gnome.org:

http://build.gnome.org/glib
Comment 15 Behdad Esfahbod 2010-03-04 15:10:02 UTC
Ugh.  Fixing.
Comment 16 Behdad Esfahbod 2010-03-04 15:40:02 UTC
Should be fixed.
Comment 17 Allison Karlitskaya (desrt) 2010-03-05 20:48:33 UTC
The patch is still causing problems.  Reopening.

Long story short: it's not possible to have functions and macros with the same name.

My suggestion: put the optimisation logic directly into the g_new() macros and have the g_malloc_n() API be pure function calls.
Comment 18 Allison Karlitskaya (desrt) 2010-03-07 04:29:03 UTC
At Behdad's request I went ahead and committed the fix.
Comment 19 Matthias Clasen 2010-03-07 21:00:06 UTC
Sure it is possible. The C standard explicitly allows this for all standard functions.
Comment 20 Behdad Esfahbod 2010-03-08 05:51:08 UTC
(In reply to comment #19)
> Sure it is possible. The C standard explicitly allows this for all standard
> functions.

Sure.  The problem is with galias.  What was happening was that the g_malloc_n macro and the galias stuff were both fighting for defining g_malloc_n as a macro to implement an optimization.  One had to go.