[ccan] [PATCH] ccanlint - avoid e.g. GPL vs LGPL mismatch in license checking

Rusty Russell rusty at rustcorp.com.au
Mon Mar 30 17:37:14 AEDT 2015


pb-ccan at barker.dropbear.id.au writes:
> From: Peter Barker <pb-ccan at barker.dropbear.id.au>

Thanks for the patch; my favourite kind of bug report!!

> +static char *xstrdup(const char *s) {
> +	char * ret = strdup(s);
> +	if (ret == NULL) {
> +		perror("strdup");
> +		abort();
> +	}
> +	return ret;
> +}
> +
> +/**
> + * line_has_license_flavour - returns true if line contains a <flavour> license
> + * @line: line to look for license in
> + * @shortname: license to find
> + * @note ("LGPLv2.0","LGPL") returns true
> + * @note ("LGPLv2.0","GPL") returns false
> + */
> +static bool line_has_license_flavour(const char *line, const char *flavour) {
> +	char *strtok_line, *strtok_tmp, *token;
> +	bool ret = false;
> +	const size_t flavour_len = strlen(flavour);
> +
> +	strtok_line = strtok_tmp = xstrdup(line);
> +	while((token = strtok(strtok_tmp, " \t-:")) != NULL) {
> +		if (!strncmp(token, flavour, flavour_len)) {
> +			ret = true;
> +			break;
> +		}
> +		strtok_tmp = NULL;
> +	}
> +	free(strtok_line);
> +
> +	return ret;
> +}

ccanlint uses tal/str, which contains the following helper:

char **tal_strsplit(const tal_t *ctx,
		    const char *string, const char *delims, enum strsplit flag);

And we have "bool strstarts(const char *str, const char *prefix)" 
in str.

Here's the patch I put on top:

commit e7d2e2fe86d10907839326748f349c9983f05779
Author: Rusty Russell <rusty at rustcorp.com.au>
Date:   Mon Mar 30 16:57:38 2015 +1030

    tools/ccanlint: slight neatening of license logic.
    
    Signed-off-by: Rusty Russell <rusty at rustcorp.com.au>

diff --git a/tools/ccanlint/tests/license_comment.c b/tools/ccanlint/tests/license_comment.c
index 88943e1..abe0204 100644
--- a/tools/ccanlint/tests/license_comment.c
+++ b/tools/ccanlint/tests/license_comment.c
@@ -10,15 +10,7 @@
 #include <stdio.h>
 #include <err.h>
 #include <ccan/str/str.h>
-
-static char *xstrdup(const char *s) {
-	char * ret = strdup(s);
-	if (ret == NULL) {
-		perror("strdup");
-		abort();
-	}
-	return ret;
-}
+#include <ccan/tal/str/str.h>
 
 /**
  * line_has_license_flavour - returns true if line contains a <flavour> license
@@ -27,21 +19,19 @@ static char *xstrdup(const char *s) {
  * @note ("LGPLv2.0","LGPL") returns true
  * @note ("LGPLv2.0","GPL") returns false
  */
-static bool line_has_license_flavour(const char *line, const char *flavour) {
-	char *strtok_line, *strtok_tmp, *token;
+static bool line_has_license_flavour(const char *line, const char *shortname)
+{
+	char **toks = tal_strsplit(NULL, line, " \t-:", STR_NO_EMPTY);
+	size_t i;
 	bool ret = false;
-	const size_t flavour_len = strlen(flavour);
 
-	strtok_line = strtok_tmp = xstrdup(line);
-	while((token = strtok(strtok_tmp, " \t-:")) != NULL) {
-		if (!strncmp(token, flavour, flavour_len)) {
+	for (i = 0; toks[i] != NULL; i++) {
+		if (strstarts(toks[i], shortname)) {
 			ret = true;
 			break;
 		}
-		strtok_tmp = NULL;
 	}
-	free(strtok_line);
-
+	tal_free(toks);
 	return ret;
 }
 


More information about the ccan mailing list