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 663633 - POSIX bindings for wordexp and fnmatch
POSIX bindings for wordexp and fnmatch
Status: RESOLVED FIXED
Product: vala
Classification: Core
Component: Bindings
unspecified
Other All
: Normal normal
: ---
Assigned To: Michael 'Mickey' Lauer
Vala maintainers
Depends on:
Blocks:
 
 
Reported: 2011-11-08 15:21 UTC by Andre Masella
Modified: 2018-03-08 09:43 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
wordexp and fnmatch bindings (1.47 KB, patch)
2011-11-08 15:22 UTC, Andre Masella
needs-work Details | Review
wordexp patch that can't leak memory (2.43 KB, patch)
2011-11-11 19:48 UTC, Andre Masella
committed Details | Review

Description Andre Masella 2011-11-08 15:21:46 UTC
POSIX bindings should include the glob-related wordexp and fnmatch.
Comment 1 Andre Masella 2011-11-08 15:22:36 UTC
Created attachment 201009 [details] [review]
wordexp and fnmatch bindings
Comment 2 Luca Bruno 2011-11-08 20:33:42 UTC
Review of attachment 201009 [details] [review]:

Thanks for the patch. The posix vapi usually tries to not change the name of the constants (FNM_NOMATCH in this case) and try to not use enums but rather const int. 
Also it's not correct to use we_wordc as length for the array, because if we_offs is used then the real length is we_wordc+we_offs. So it must be an array without length from a Vala view point unfortunately.
Comment 3 Andre Masella 2011-11-08 21:01:40 UTC
(In reply to comment #2)
> Thanks for the patch. The posix vapi usually tries to not change the name of
> the constants (FNM_NOMATCH in this case) and try to not use enums but rather
> const int.

Okay. I was trying to be typesafe. ;-) Should I put WRDE_x as just x inside struct word?

> Also it's not correct to use we_wordc as length for the array, because if
> we_offs is used then the real length is we_wordc+we_offs. So it must be an
> array without length from a Vala view point unfortunately.

Actually, I realized there is no point to having we_offs at all. we_offs must be set by the user before wordexp is called with WRDE_DOOFFS, which isn't possible with it being an out parameter. So, I could change it to ref and allow this or leave it an drop WRDE_DOOFFS. Which is preferred? Is it possible to make this work via a array_length_cexpr? (It seems like what that's for, but I've not found sample code using it.)
Comment 4 Luca Bruno 2011-11-08 21:35:23 UTC
(In reply to comment #3)
> Okay. I was trying to be typesafe. ;-) Should I put WRDE_x as just x inside
> struct word?

WRDE should be fine.

> Actually, I realized there is no point to having we_offs at all. we_offs must
> be set by the user before wordexp is called with WRDE_DOOFFS, which isn't
> possible with it being an out parameter. So, I could change it to ref and allow
> this or leave it an drop WRDE_DOOFFS. Which is preferred? Is it possible to
> make this work via a array_length_cexpr? (It seems like what that's for, but
> I've not found sample code using it.)

Try making it an instance method with CCode (instance_pos = 1.1) or such (see Glob for reference).
Comment 5 Andre Masella 2011-11-08 22:01:04 UTC
(In reply to comment #4)
> Try making it an instance method with CCode (instance_pos = 1.1) or such (see
> Glob for reference).

Okay, but if you call it multiple times, you leak memory. In the case of wordexp, you can use WRDE_REUSE to realloc. glob leaks memory as it is codified now.

Try:
var g = Posix.Glob();
g.glob("*.vala");
g.glob("*.vala");

and valgrind says:
==16064== 53 (24 direct, 29 indirect) bytes in 1 blocks are definitely lost in loss record 4 of 4
==16064==    at 0x4C28F9F: malloc (vg_replace_malloc.c:236)
==16064==    by 0x4C29019: realloc (vg_replace_malloc.c:525)
==16064==    by 0x4EDEF62: glob_in_dir (glob.c:1458)
==16064==    by 0x4EE02C7: glob (glob.c:1008)
==16064==    by 0x4009F6: _vala_main (in /home/andre/Documents/Coding/vala/wrde)
==16064==    by 0x400A57: main (in /home/andre/Documents/Coding/vala/wrde)
Comment 6 Luca Bruno 2011-11-08 22:07:22 UTC
That's true. It is weird api in either ways. The offset is useful for adding stuff before in C, but maybe in Vala it doesn't apply and we could drop offsets.
Comment 7 Luca Bruno 2011-11-08 22:08:40 UTC
(In reply to comment #5)
> Okay, but if you call it multiple times, you leak memory. In the case of
> wordexp, you can use WRDE_REUSE to realloc. glob leaks memory as it is codified
> now.
> 
> Try:
> var g = Posix.Glob();
> g.glob("*.vala");
> g.glob("*.vala");

I forgot to say in glob it's possible to use GLOB_APPEND. In which case that works correctly.
Comment 8 Andre Masella 2011-11-08 22:15:20 UTC
O(In reply to comment #7)
> (In reply to comment #5)
> > Okay, but if you call it multiple times, you leak memory. In the case of
> > wordexp, you can use WRDE_REUSE to realloc. glob leaks memory as it is codified
> > now.
> I forgot to say in glob it's possible to use GLOB_APPEND. In which case that
> works correctly.

Okay. What about this:
Make wordexp and glob be private methods with wrappers that have the appropriate flags. Something like:

struct word {
private int _wordexp(string pattern, word w, int flags);
public int again(string pattern, int flags) {
return _wordexp(pattern, this, flags | REUSE);
}

Though, that's kind of pointless for wordexp since calling it as an out parameter will call wordfree. Glob should probably be changed to be similar to the above.

I'm in favour of dropping the offsets.
Comment 9 Andre Masella 2011-11-11 19:48:29 UTC
Created attachment 201251 [details] [review]
wordexp patch that can't leak memory
Comment 10 Andre Masella 2011-11-11 19:50:31 UTC
The new version should be more straight forward to use and won't leak memory in that way. However, I haven't found a good solution for glob. I tried the same thing, but because the GlobErrorFunc has no cname, there is no obvious solution.