Discussion:
fix some gcc warnings
(too old to reply)
Bruno Haible
2016-10-16 15:01:56 UTC
Permalink
Raw Message
Hi,

When I compile a gnulib testdir with "gcc -Wall", I see the following warnings
(among others):

test-ffsl.c: In function 'main':
test-ffsl.c:44:12: warning: unused variable 'x' [-Wunused-variable]

test-posix_spawn3.c: In function 'parent_main':
test-posix_spawn3.c:78:11: warning: ignoring return value of 'freopen', declared with attribute warn_unused_result [-Wunused-result]

test-sethostname1.c: In function 'main':
test-sethostname1.c:34:17: warning: ignoring return value of 'sethostname', declared with attribute warn_unused_result [-Wunused-result]

Here's a proposed fix for them.


2016-10-16 Bruno Haible <***@clisp.org>

Fix some "gcc -Wall" warnings.
* tests/test-ffsl.c (main): Use variable x, not i.
* tests/test-posix_spawn3.c (parent_main): Consider the return value of
freopen.
* tests/test-sethostname1.c (main): Explicitly ignore the return value
of sethostname.

diff --git a/tests/test-ffsl.c b/tests/test-ffsl.c
index e903509..117bc4a 100644
--- a/tests/test-ffsl.c
+++ b/tests/test-ffsl.c
@@ -44,8 +44,8 @@ main (int argc, char *argv[])
long int x;
int i;

- for (i = -128; i <= 128; i++)
- ASSERT (ffsl (i) == naive (i));
+ for (x = -128; x <= 128; x++)
+ ASSERT (ffsl (x) == naive (x));
for (i = 0; i < NBITS; i++)
{
ASSERT (ffsl (1UL << i) == naive (1UL << i));
diff --git a/tests/test-posix_spawn3.c b/tests/test-posix_spawn3.c
index 90745fd..f2a699c 100644
--- a/tests/test-posix_spawn3.c
+++ b/tests/test-posix_spawn3.c
@@ -75,7 +75,11 @@ parent_main (void)
}

/* Avoid reading from our stdin, as it could block. */
- freopen ("/dev/null", "rb", stdin);
+ if (freopen ("/dev/null", "rb", stdin) == NULL)
+ {
+ perror ("cannot redirect stdin");
+ return 1;
+ }

/* Test whether posix_spawn_file_actions_addopen with this file name
actually works, but spawning a child that reads from this file. */
diff --git a/tests/test-sethostname1.c b/tests/test-sethostname1.c
index 5ed6a83..08441f3 100644
--- a/tests/test-sethostname1.c
+++ b/tests/test-sethostname1.c
@@ -30,8 +30,11 @@ main ()
/* Some code that has a link-time dependency to the sethostname() function
and that is likely not optimized away by compilers. */
if (do_dangerous_things)
- /* Never executed. */
- sethostname ("oprah", 5);
+ {
+ /* Never executed. */
+ int ret = sethostname ("oprah", 5);
+ (void) ret;
+ }

return 0;
}
Pádraig Brady
2016-10-16 15:52:18 UTC
Permalink
Raw Message
Post by Bruno Haible
Hi,
When I compile a gnulib testdir with "gcc -Wall", I see the following warnings
test-ffsl.c:44:12: warning: unused variable 'x' [-Wunused-variable]
test-posix_spawn3.c:78:11: warning: ignoring return value of 'freopen', declared with attribute warn_unused_result [-Wunused-result]
test-sethostname1.c:34:17: warning: ignoring return value of 'sethostname', declared with attribute warn_unused_result [-Wunused-result]
Here's a proposed fix for them.
Fix some "gcc -Wall" warnings.
* tests/test-ffsl.c (main): Use variable x, not i.
* tests/test-posix_spawn3.c (parent_main): Consider the return value of
freopen.
* tests/test-sethostname1.c (main): Explicitly ignore the return value
of sethostname.
diff --git a/tests/test-ffsl.c b/tests/test-ffsl.c
index e903509..117bc4a 100644
--- a/tests/test-ffsl.c
+++ b/tests/test-ffsl.c
@@ -44,8 +44,8 @@ main (int argc, char *argv[])
long int x;
int i;
- for (i = -128; i <= 128; i++)
- ASSERT (ffsl (i) == naive (i));
+ for (x = -128; x <= 128; x++)
+ ASSERT (ffsl (x) == naive (x));
for (i = 0; i < NBITS; i++)
{
ASSERT (ffsl (1UL << i) == naive (1UL << i));
diff --git a/tests/test-posix_spawn3.c b/tests/test-posix_spawn3.c
index 90745fd..f2a699c 100644
--- a/tests/test-posix_spawn3.c
+++ b/tests/test-posix_spawn3.c
@@ -75,7 +75,11 @@ parent_main (void)
}
/* Avoid reading from our stdin, as it could block. */
- freopen ("/dev/null", "rb", stdin);
+ if (freopen ("/dev/null", "rb", stdin) == NULL)
+ {
+ perror ("cannot redirect stdin");
+ return 1;
+ }
/* Test whether posix_spawn_file_actions_addopen with this file name
actually works, but spawning a child that reads from this file. */
diff --git a/tests/test-sethostname1.c b/tests/test-sethostname1.c
index 5ed6a83..08441f3 100644
--- a/tests/test-sethostname1.c
+++ b/tests/test-sethostname1.c
@@ -30,8 +30,11 @@ main ()
/* Some code that has a link-time dependency to the sethostname() function
and that is likely not optimized away by compilers. */
if (do_dangerous_things)
- /* Never executed. */
- sethostname ("oprah", 5);
+ {
+ /* Never executed. */
+ int ret = sethostname ("oprah", 5);
+ (void) ret;
+ }
return 0;
}
+1
thanks
Bruno Haible
2016-10-22 17:30:17 UTC
Permalink
Raw Message
Post by Pádraig Brady
Post by Bruno Haible
Fix some "gcc -Wall" warnings.
* tests/test-ffsl.c (main): Use variable x, not i.
* tests/test-posix_spawn3.c (parent_main): Consider the return value of
freopen.
* tests/test-sethostname1.c (main): Explicitly ignore the return value
of sethostname.
+1
thanks
OK, I pushed this.

Loading...