[PATCH] state: check return value of chown

yeholmes at outlook.com yeholmes at outlook.com
Tue Jan 5 06:58:48 EST 2021


On Mon, 04 Jan 2021 20:20:58 -0000, Karl Palsson wrote:
> This is akin to the classic problem of logging macros turning off
> code. You need to introduce an extra variable for the assert to
> check. (If you want to keep going down this path)

Thank you very much for your 'junk' code :) I've tested
that introducing an extra variables sometimes causes
another unused warning, which is unfortunately treated
as error by gcc. Sorry for proposing such problematic patch!

#include <assert.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>

static int k;

void call_it_safe(void) {
	int ret;
	assert(++k > 0);
	errno = 0;
	ret = chown("/non-exist-directory", 1000, 1002);
	assert(ret == 0);
	fprintf(stderr, "errno: %d\n", errno);
	fflush(stderr);
}
gcc -Wall -Werror -DNDEBUG -o hate_ndebug hate.c
hate.c: In function ‘call_it_safe’:
hate.c:10:6: error: variable ‘ret’ set but not used [-Werror=unused-but-set-variable]
   10 |  int ret;
      |      ^~~
cc1: all warnings being treated as errors



More information about the openwrt-devel mailing list