]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
tests: fclose(stdin) UB
authorCristian Rodríguez <crrodriguez@opensuse.org>
Tue, 21 Mar 2023 19:55:15 +0000 (19:55 +0000)
committerCristian Rodríguez <crrodriguez@opensuse.org>
Tue, 21 Mar 2023 19:55:15 +0000 (19:55 +0000)
The standard streams must not be closed unless it is
the very last thing a program does before termination.

A warning about this was added to POSIX.1-2008 and later standards.

One must redirect stdin to /dev/null using freopen instead.

tests/helpers/test_md5.c
tests/helpers/test_sha1.c

index 8f2f686bcfed7415af29d3deba3fa11749c667c9..dc49d2184a0605625473f2b5ecb5d10884358b22 100644 (file)
@@ -5,6 +5,8 @@
  */
 #include <stdio.h>
 #include <unistd.h>
+#include <stdlib.h>
+#include <err.h>
 
 #include "md5.h"
 
@@ -23,7 +25,9 @@ int main(void)
                        ul_MD5Update( &ctx, buf, ret );
        }
 
-       fclose(stdin);
+       if(freopen ("/dev/null", "r", stdin) == NULL)
+               err(EXIT_FAILURE, "stdin->null failed!");
+
        ul_MD5Final( digest, &ctx );
 
        for (i = 0; i < UL_MD5LENGTH; i++)
index e2a5f0199d49c3e3721d4105352cdc6cc400f4b3..7cb107f8752c22e8747c173bc53577d6d458a66e 100644 (file)
@@ -5,6 +5,8 @@
  */
 #include <stdio.h>
 #include <unistd.h>
+#include <err.h>
+#include <stdlib.h>
 
 #include "sha1.h"
 
@@ -23,7 +25,9 @@ int main(void)
                        ul_SHA1Update( &ctx, buf, ret );
        }
 
-       fclose(stdin);
+       if(freopen ("/dev/null", "r", stdin) == NULL)
+               err(EXIT_FAILURE, "stdin->null failed!");
+
        ul_SHA1Final( digest, &ctx );
 
        for (i = 0; i < UL_SHA1LENGTH; i++)