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 653005 - GMatchInfo and the lifetime of the matched stirng
GMatchInfo and the lifetime of the matched stirng
Status: RESOLVED OBSOLETE
Product: glib
Classification: Platform
Component: gregex
unspecified
Other Linux
: Normal enhancement
: ---
Assigned To: gtkdev
gtkdev
Depends on:
Blocks:
 
 
Reported: 2011-06-20 13:42 UTC by Christian Persch
Modified: 2018-05-24 13:12 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
Add g_match_info_[sg]et_data (5.95 KB, patch)
2011-06-20 13:42 UTC, Christian Persch
none Details | Review

Description Christian Persch 2011-06-20 13:42:13 UTC
When creating a GMatchInfo using the g_regex_match*() functions, the matched string is not copied, so it needs to be kept alive as long as the match info is used.

This is a problem when the string was created from other data just to be matched, and the match info is then passed around (e.g. passed out via public APIs), which makes it impossible to manage the string's lifecycle. (I have this problem in vte.)

I can think of a few ways to solve this:
1) Add a G_REGEX_MATCH_TAKE_STRING flag to GRegexMatchFlags, that when passed to g_regex_match*() makes the match info own the string, and free it using g_free(). (The problem here is that it hardcodes the destroy notify to g_free().)
2) Add new g_regex_match_fuller() (since match_full is already taken) that takes an additional (data, destroy notify) pair.
3) Add qdata to GMatchInfo, so I can store the string there with an appropriate destroy notify.
3a) Add g_match_info_set_data(GMatchInfo*, gpointer, GDestroyNotify), since I really need just one pointer, not a whole family of qdatas.

3a) is the simplest solution; attached patch implements it.
Comment 1 Christian Persch 2011-06-20 13:42:33 UTC
Created attachment 190271 [details] [review]
Add g_match_info_[sg]et_data

The string passed to g_regex_match*() needs to live as long as the
returned GMatchInfo is used. When passing the match info around, and
the string was generated just to be matched, this makes managing the
lifecycle of the string difficult. By adding g_match_info_set_data(),
the string's lifetime can be tied to the lifetime of the match info.

Bug #653005.
Comment 2 Behdad Esfahbod 2011-06-23 15:37:31 UTC
This is why we should make everything a GObject...  It's weird that GMatchInfo doesn't even have a refcount.
Comment 3 Behdad Esfahbod 2011-06-23 15:38:46 UTC
GMatchInfo neither has a ref function nor a copy.  How is it boxed then?
Comment 4 Christian Persch 2011-06-23 16:00:56 UTC
It doesn't have a boxed GType either. Do you think it should have one, and ref/unref funcs?
Comment 5 Behdad Esfahbod 2011-06-23 16:03:23 UTC
I understand that the gregex stuff is considered mostly-C-only because every other language has its own regex.  But when we have vte API returning GRegex or match-info, it needs to be bindable, right?
Comment 6 Christian Persch 2011-06-23 16:09:52 UTC
I guess so, yes. The boxed type for GRegex was added way back in bug 445065, but strangely no mention was made of GMatchInfo then.
Comment 7 Christian Persch 2011-06-23 16:42:38 UTC
Filed that as bug 653248.
Comment 8 Christian Persch 2011-08-03 13:12:07 UTC
(In reply to comment #2)
> This is why we should make everything a GObject...  It's weird that GMatchInfo
> doesn't even have a refcount.

Yes, but that's impossible since gregex is in libglib and thus cannot be a gobject. At least it's now refcounted (from bug 653248).

Any opinion from the glib maintainers on the patch here and/or on the different possibilities in comment 0 ?
Comment 9 Christian Persch 2012-02-10 13:36:44 UTC
Ping? Any chance of getting this into 2.32 ?
Comment 10 Matthias Clasen 2012-02-10 14:34:28 UTC
I'l try to have a look
Comment 11 Christian Persch 2012-07-11 21:11:15 UTC
I'm now leaning more towards 2), a g_regex_match_full variant perhaps called g_regex_match_take_string() (no need to expose a take-string non-full version as well, since this is kind of a specialised use case anyway).
Comment 12 GNOME Infrastructure Team 2018-05-24 13:12:40 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/419.