Subject: Re: [PATCH v3 2/3] t0021: implementation the rot13-filter.pl script in C

From: Ævar Arnfjörð Bjarmason <[email-masked]>
Date: Mon, 01 Aug 2022 13:33:36 +0200

On Sun, Jul 31 2022, Matheus Tavares wrote:

> +static char *rot13_usage = "test-tool rot13-filter [--always-delay] <log path> <capabilities>";
> +
> +int cmd__rot13_filter(int argc, const char **argv)
> +{
> +	const char **caps;
> +	int cap_count, i = 1;
> +	struct strset remote_caps = STRSET_INIT;
> +
> +	if (argc > 1 && !strcmp(argv[1], "--always-delay")) {
> +		always_delay = 1;
> +		i++;
> +	}
> +	if (argc - i < 2)
> +		usage(rot13_usage);
> +
> +	logfile = fopen(argv[i++], "a");
> +	if (!logfile)
> +		die_errno("failed to open log file");
> +
> +	caps = argv + i;
> +	cap_count = argc - i;

Since you need to change every single caller consider just starting out
with parse_options() here instead of rolling your own parsing. You could
use it for --always-delay in any case, but you could also just add a
--log-path and --capability (an OPT_STRING_LIST), so:

	test-tool rot13-filter [--always-delay] --log-path=<path> [--capability <capbility]...

> +
> +	for (i = 0; i < cap_count; i++) {
> +		if (!strcmp(caps[i], "clean"))
> +			has_clean_cap = 1;
> +		else if (!strcmp(caps[i], "smudge"))
> +			has_smudge_cap = 1;

In any case, maybe BUG() in an "else" here with "unknown capability"?

> +	fclose(logfile);

Perhaps check the return value & die_errno() if we fail to fclose()
(happens e.g. if the disk fills up).