diff mbox

[LEDE-DEV,uclient] Fix unused results warnings

Message ID 20161203175855.6262-1-f.fainelli@gmail.com
State Changes Requested
Headers show

Commit Message

Florian Fainelli Dec. 3, 2016, 5:58 p.m. UTC
Fixes:

uclient-http.c:385:8: error: ignoring return value of 'fread', declared with attribute warn_unused_result [-Werror=unused-result]
   fread(&val, sizeof(val), 1, f);
        ^

uclient-fetch.c: In function 'main':
uclient-fetch.c:664:12: error: ignoring return value of 'asprintf', declared with attribute warn_unused_result [-Werror=unused-result]
    asprintf(&auth_str, "%s:%s", username, password);
            ^
uclient-fetch.c: In function 'read_data_cb':
uclient-fetch.c:269:9: error: ignoring return value of 'write', declared with attribute warn_unused_result [-Werror=unused-result]
    write(output_fd, buf, len);

Signed-off-by: Florian Fainelli <f.fainelli@gmail.com>
---
 uclient-fetch.c | 16 +++++++++++-----
 uclient-http.c  |  5 ++++-
 2 files changed, 15 insertions(+), 6 deletions(-)

Comments

John Crispin Dec. 4, 2016, 3:14 p.m. UTC | #1
On 03/12/2016 18:58, Florian Fainelli wrote:
> Fixes:
> 
> uclient-http.c:385:8: error: ignoring return value of 'fread', declared with attribute warn_unused_result [-Werror=unused-result]
>    fread(&val, sizeof(val), 1, f);
>         ^
> 
> uclient-fetch.c: In function 'main':
> uclient-fetch.c:664:12: error: ignoring return value of 'asprintf', declared with attribute warn_unused_result [-Werror=unused-result]
>     asprintf(&auth_str, "%s:%s", username, password);
>             ^
> uclient-fetch.c: In function 'read_data_cb':
> uclient-fetch.c:269:9: error: ignoring return value of 'write', declared with attribute warn_unused_result [-Werror=unused-result]
>     write(output_fd, buf, len);
> 
> Signed-off-by: Florian Fainelli <f.fainelli@gmail.com>
> ---
>  uclient-fetch.c | 16 +++++++++++-----
>  uclient-http.c  |  5 ++++-
>  2 files changed, 15 insertions(+), 6 deletions(-)
> 
> diff --git a/uclient-fetch.c b/uclient-fetch.c
> index 4c603fbc1945..db13e81bf8d0 100644
> --- a/uclient-fetch.c
> +++ b/uclient-fetch.c
> @@ -254,6 +254,7 @@ static void header_done_cb(struct uclient *cl)
>  static void read_data_cb(struct uclient *cl)
>  {
>  	char buf[256];
> +	size_t n;
>  	int len;
>  
>  	if (!no_output && output_fd < 0)
> @@ -265,8 +266,11 @@ static void read_data_cb(struct uclient *cl)
>  			return;
>  
>  		out_bytes += len;
> -		if (!no_output)
> -			write(output_fd, buf, len);
> +		if (!no_output) {
> +			n = write(output_fd, buf, len);
> +			if (n < len)
> +				return;
> +		}
>  	}
>  }
>  
> @@ -660,9 +664,11 @@ int main(int argc, char **argv)
>  	uloop_init();
>  
>  	if (username) {
> -		if (password)
> -			asprintf(&auth_str, "%s:%s", username, password);
> -		else
> +		if (password) {
> +			rc = asprintf(&auth_str, "%s:%s", username, password);
> +			if (rc < 0)
> +				return rc;
> +		} else
>  			auth_str = username;
>  	}
>  
> diff --git a/uclient-http.c b/uclient-http.c
> index 8d26bd4884be..80f40d0e40e3 100644
> --- a/uclient-http.c
> +++ b/uclient-http.c
> @@ -379,11 +379,14 @@ get_cnonce(char *dest)
>  {
>  	uint32_t val = 0;
>  	FILE *f;
> +	size_t n;
>  
>  	f = fopen("/dev/urandom", "r");
>  	if (f) {
> -		fread(&val, sizeof(val), 1, f);
> +		n = fread(&val, sizeof(val), 1, f);
>  		fclose(f);
> +		if (n != sizeof(val))
> +			return;
>  	}

from the man page "On success, fread() and fwrite() return the number of
items read or written.  This number equals the number of bytes
transferred only when size is 1.  If an error occurs, or the end of the
file  is  reached,  the  return value is a short item count (or zero)."
so the check should be (n != 1) i think.

	John


>  
>  	bin_to_hex(dest, &val, sizeof(val));
>
Florian Fainelli Dec. 4, 2016, 9:02 p.m. UTC | #2
Le 12/04/16 à 07:14, John Crispin a écrit :
> 
> 
> On 03/12/2016 18:58, Florian Fainelli wrote:
>> Fixes:
>>
>> uclient-http.c:385:8: error: ignoring return value of 'fread', declared with attribute warn_unused_result [-Werror=unused-result]
>>    fread(&val, sizeof(val), 1, f);
>>         ^
>>
>> uclient-fetch.c: In function 'main':
>> uclient-fetch.c:664:12: error: ignoring return value of 'asprintf', declared with attribute warn_unused_result [-Werror=unused-result]
>>     asprintf(&auth_str, "%s:%s", username, password);
>>             ^
>> uclient-fetch.c: In function 'read_data_cb':
>> uclient-fetch.c:269:9: error: ignoring return value of 'write', declared with attribute warn_unused_result [-Werror=unused-result]
>>     write(output_fd, buf, len);
>>
>> Signed-off-by: Florian Fainelli <f.fainelli@gmail.com>
>> ---
>>  uclient-fetch.c | 16 +++++++++++-----
>>  uclient-http.c  |  5 ++++-
>>  2 files changed, 15 insertions(+), 6 deletions(-)
>>
>> diff --git a/uclient-fetch.c b/uclient-fetch.c
>> index 4c603fbc1945..db13e81bf8d0 100644
>> --- a/uclient-fetch.c
>> +++ b/uclient-fetch.c
>> @@ -254,6 +254,7 @@ static void header_done_cb(struct uclient *cl)
>>  static void read_data_cb(struct uclient *cl)
>>  {
>>  	char buf[256];
>> +	size_t n;
>>  	int len;
>>  
>>  	if (!no_output && output_fd < 0)
>> @@ -265,8 +266,11 @@ static void read_data_cb(struct uclient *cl)
>>  			return;
>>  
>>  		out_bytes += len;
>> -		if (!no_output)
>> -			write(output_fd, buf, len);
>> +		if (!no_output) {
>> +			n = write(output_fd, buf, len);
>> +			if (n < len)
>> +				return;
>> +		}
>>  	}
>>  }
>>  
>> @@ -660,9 +664,11 @@ int main(int argc, char **argv)
>>  	uloop_init();
>>  
>>  	if (username) {
>> -		if (password)
>> -			asprintf(&auth_str, "%s:%s", username, password);
>> -		else
>> +		if (password) {
>> +			rc = asprintf(&auth_str, "%s:%s", username, password);
>> +			if (rc < 0)
>> +				return rc;
>> +		} else
>>  			auth_str = username;
>>  	}
>>  
>> diff --git a/uclient-http.c b/uclient-http.c
>> index 8d26bd4884be..80f40d0e40e3 100644
>> --- a/uclient-http.c
>> +++ b/uclient-http.c
>> @@ -379,11 +379,14 @@ get_cnonce(char *dest)
>>  {
>>  	uint32_t val = 0;
>>  	FILE *f;
>> +	size_t n;
>>  
>>  	f = fopen("/dev/urandom", "r");
>>  	if (f) {
>> -		fread(&val, sizeof(val), 1, f);
>> +		n = fread(&val, sizeof(val), 1, f);
>>  		fclose(f);
>> +		if (n != sizeof(val))
>> +			return;
>>  	}
> 
> from the man page "On success, fread() and fwrite() return the number of
> items read or written.  This number equals the number of bytes
> transferred only when size is 1.  If an error occurs, or the end of the
> file  is  reached,  the  return value is a short item count (or zero)."
> so the check should be (n != 1) i think.

Humm yes good point, thanks!
diff mbox

Patch

diff --git a/uclient-fetch.c b/uclient-fetch.c
index 4c603fbc1945..db13e81bf8d0 100644
--- a/uclient-fetch.c
+++ b/uclient-fetch.c
@@ -254,6 +254,7 @@  static void header_done_cb(struct uclient *cl)
 static void read_data_cb(struct uclient *cl)
 {
 	char buf[256];
+	size_t n;
 	int len;
 
 	if (!no_output && output_fd < 0)
@@ -265,8 +266,11 @@  static void read_data_cb(struct uclient *cl)
 			return;
 
 		out_bytes += len;
-		if (!no_output)
-			write(output_fd, buf, len);
+		if (!no_output) {
+			n = write(output_fd, buf, len);
+			if (n < len)
+				return;
+		}
 	}
 }
 
@@ -660,9 +664,11 @@  int main(int argc, char **argv)
 	uloop_init();
 
 	if (username) {
-		if (password)
-			asprintf(&auth_str, "%s:%s", username, password);
-		else
+		if (password) {
+			rc = asprintf(&auth_str, "%s:%s", username, password);
+			if (rc < 0)
+				return rc;
+		} else
 			auth_str = username;
 	}
 
diff --git a/uclient-http.c b/uclient-http.c
index 8d26bd4884be..80f40d0e40e3 100644
--- a/uclient-http.c
+++ b/uclient-http.c
@@ -379,11 +379,14 @@  get_cnonce(char *dest)
 {
 	uint32_t val = 0;
 	FILE *f;
+	size_t n;
 
 	f = fopen("/dev/urandom", "r");
 	if (f) {
-		fread(&val, sizeof(val), 1, f);
+		n = fread(&val, sizeof(val), 1, f);
 		fclose(f);
+		if (n != sizeof(val))
+			return;
 	}
 
 	bin_to_hex(dest, &val, sizeof(val));