26 ms·
Not a question, a request: Please make __attribute__((cleanup)) or the equivalent feature part of the next C standard. It's used by a lot of current software i
by rwmj 7y ago
Not a question, a request: Please make __attribute__((cleanup)) or the equivalent feature part of the next C standard.
It's used by a lot of current software in Linux, notably systemd and glib2. It solves a major headache with C error handling elegantly. Most compilers already support it internally (since it's required by C++). It has predictable effects, and no impact on performance when not used. It cannot be implemented without help from the compiler.
- AaronBallman 7y agoFunny you should mention that, as that feature has come up recently in mailing list discussions. We have not seen an actual proposal for adopting it yet, but features similar semantics are being discussed as a possible idea (no promises). FWIW, I don't think it would wind up being spelled with attribute syntax because we would likely want programmers to have a guarantee that the cleanup will happen (and attributes can be ignored by the implementation).
- eqvinox 7y agoHopefully it'd at least be syntactically similar, so we can have an #ifdef __STDC_CLEANUP__ #define my_cleanup(func) stdc_cleanup(func) #else #define my_cleanup(func) __attribute__((cleanup(func))) #endif i.e. it would require that it at least goes in the same places as an attribute.
- rwmj 7y agoI believe the last proposal was in 2008 (ignore the try..finally stuff here): http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1298.pdf http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1298.pdf So I guess it needs someone to take that and update it, also to pull up a full list of current Linux software which is using this feature (which as I say these days is a surprising amount).
- eqvinox 7y agoHere's our usage: https://github.com/FRRouting/frr/blob/master/lib/privs.h#L141-L146 https://github.com/FRRouting/frr/blob/master/lib/privs.h#L14... #define frr_with_privs(privs) \ for (struct zebra_privs_t *_once = NULL, \ *_privs __attribute__( \ (unused, cleanup(_zprivs_lower))) = \ _zprivs_raise(privs, __func__); \ _once == NULL; _once = (void *)1) This gives us a block construct that guarantees elevated privileges are dropped when the block is done: frr_with_privs(privs) { ... whatever ... break; /* exit block, drop privileges */ return; /* return, drop privileges */ }
- rwmj 7y agoWe have a nice macro for acquiring locks that only applies to the scope: https://github.com/libguestfs/nbdkit/blob/e58d28d65bfea3af3694502c4030838f3cf5d6e8/common/utils/cleanup.h#L44 https://github.com/libguestfs/nbdkit/blob/e58d28d65bfea3af36... You end up with code like this: https://github.com/libguestfs/nbdkit/blob/e58d28d65bfea3af3694502c4030838f3cf5d6e8/server/protocol.c#L369 https://github.com/libguestfs/nbdkit/blob/e58d28d65bfea3af36... It's so useful to be able to be sure the lock is released on all return paths. Also because it's scope-level you can scope your locks tightly to where they are needed.
- loeg 7y agoWe use it extensively in our proprietary codebases as well, FWIW. Not real open data for me to point to, but: a few million lines of C, and a handful of billion USD in revenue. If that helps weigh in on "yes, please standardize this common practice."
- rseacord 7y agoMy idea was to add something like the GoLang defer statement to C (as a function with some special compiler magic). The following is an example of how such a function could be used to cleanup allocated resources regardless of how a function returned: int do_something(void) { FILE *file1, *file2; object_t *obj; file1 = fopen("a_file", "w"); if (file1 == NULL) { return -1; } defer(fclose, file1); file2 = fopen("another_file", "w"); if (file2 == NULL) { return -1; } defer(fclose, file2); obj = malloc(sizeof(object_t)); if (obj == NULL) { return -1; } // Operate on allocated resources // Clean up everything free(obj); // this could be deferred too, I suppose, for symmetry return 0; }
- eqvinox 7y agoCleanup on function return is not enough, it needs to be scope exit. We're using this for privilege raising/dropping (example posted above) and also mutex acquisition/release. Both of these really "want" it on the scope level.
- rwmj 7y agoGolang gets this wrong. It should be scope-level not function-level (or perhaps there should be two different types, but I have never personally had a need for a function-level cleanup). Edit: Also please review how attribute cleanup is used by existing C code before jumping into proposals. If something is added to C2x which is inconsistent with what existing code is already doing widely, then it's no help to anyone.
- rseacord 7y agoYes, we have discussed adding this feature at scope level. A not entirely serious proposal was to implement it as follows: #define DEFER(a, b, c) \ for (bool _flag = true; _flag; _flag = false) \ for (a; _flag && (b); c, _flag = false) int fun() { DEFER(FILE *f1 = fopen(...), (NULL != f1), mfclose(f1)) { DEFER(FILE *f2 = fopen(...), (NULL != f2), mfclose(f2)) { DEFER(FILE *f3 = fopen(...), (NULL != f3), mfclose(f3)) { ... do something ... } } } } We are also looking at the attribute cleanup. Sounds like you should be involved in developing this proposal?