]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
contrib/cleanup: be more correct
authorVladimír Čunát <vladimir.cunat@nic.cz>
Wed, 7 Oct 2020 07:51:16 +0000 (09:51 +0200)
committerVladimír Čunát <vladimir.cunat@nic.cz>
Wed, 7 Oct 2020 07:51:16 +0000 (09:51 +0200)
It's mainly about the fact that FD can be zero (though it's not common).
Our current usage is just in tests and seems fine.

I don't think that other negative FDs are possible, but I'm lazy to find
"proof" in POSIX and using other negative values than -1 doesn't make
sense to me anyway (might be an assert, I guess).

contrib/cleanup.h

index ba5343f63fb51ddaf4a9f899aa04b6acfddbc86f..1a51aad383271a50134fb338c40f68763c8e32f8 100644 (file)
@@ -16,7 +16,7 @@ static inline void _cleanup_free(char **p) {
 }
 #define auto_close __attribute__((cleanup(_cleanup_close)))
 static inline void _cleanup_close(int *p) {
-       if (*p > 0) close(*p);
+       if (*p != -1) close(*p);
 }
 #define auto_fclose __attribute__((cleanup(_cleanup_fclose)))
 static inline void _cleanup_fclose(FILE **p) {