# [PATCH] test-path-utils: handle const parameter of basename and dirname

2 messages from 2017-08-07 to 2017-08-07. Participants: René Scharfe, Johannes Schindelin.
Thread: https://gitlist.dev/t/46527

## René Scharfe, 2017-08-07 13:57

Subject: [PATCH] test-path-utils: handle const parameter of basename and dirname
Message-ID: <5bfd3c7c-c3c8-5115-713a-63f6f8cd7eb7@web.de>
URL: https://gitlist.dev/e/5bfd3c7c-c3c8-5115-713a-63f6f8cd7eb7%40web.de

```
The parameter to basename(3) and dirname(3) traditionally had the type
"char *", but on OpenBSD it's been "const char *" for years.  That
causes (at least) Clang to throw an incompatible-pointer-types warning
for test-path-utils, where we try to pass around pointers to these
functions.

Avoid this warning (which is fatal in DEVELOPER mode) by ignoring the
promise of OpenBSD's implementations to keep input strings unmodified
and enclosing them in POSIX-compatible wrappers.

Signed-off-by: Rene Scharfe <l.s.r@web.de>
---
 t/helper/test-path-utils.c | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)

diff --git a/t/helper/test-path-utils.c b/t/helper/test-path-utils.c
index 1ebe0f750c..2b3c5092a1 100644
--- a/t/helper/test-path-utils.c
+++ b/t/helper/test-path-utils.c
@@ -38,6 +38,20 @@ struct test_data {
 	const char *alternative; /* output: ... or this.      */
 };
 
+/*
+ * Compatibility wrappers for OpenBSD, whose basename(3) and dirname(3)
+ * have const parameters.
+ */
+static char *posix_basename(char *path)
+{
+	return basename(path);
+}
+
+static char *posix_dirname(char *path)
+{
+	return dirname(path);
+}
+
 static int test_function(struct test_data *data, char *(*func)(char *input),
 	const char *funcname)
 {
@@ -251,10 +265,10 @@ int cmd_main(int argc, const char **argv)
 	}
 
 	if (argc == 2 && !strcmp(argv[1], "basename"))
-		return test_function(basename_data, basename, argv[1]);
+		return test_function(basename_data, posix_basename, argv[1]);
 
 	if (argc == 2 && !strcmp(argv[1], "dirname"))
-		return test_function(dirname_data, dirname, argv[1]);
+		return test_function(dirname_data, posix_dirname, argv[1]);
 
 	fprintf(stderr, "%s: unknown function name: %s\n", argv[0],
 		argv[1] ? argv[1] : "(there was none)");
-- 
2.14.0


```

## Johannes Schindelin, 2017-08-07 21:15

Subject: Re: [PATCH] test-path-utils: handle const parameter of basename and dirname
Message-ID: <alpine.DEB.2.21.1.1708072314420.4271@virtualbox>
URL: https://gitlist.dev/e/alpine.DEB.2.21.1.1708072314420.4271%40virtualbox
In-Reply-To: <5bfd3c7c-c3c8-5115-713a-63f6f8cd7eb7@web.de>

```
Hi René,

On Mon, 7 Aug 2017, René Scharfe wrote:

> The parameter to basename(3) and dirname(3) traditionally had the type
> "char *", but on OpenBSD it's been "const char *" for years.  That
> causes (at least) Clang to throw an incompatible-pointer-types warning
> for test-path-utils, where we try to pass around pointers to these
> functions.
> 
> Avoid this warning (which is fatal in DEVELOPER mode) by ignoring the
> promise of OpenBSD's implementations to keep input strings unmodified
> and enclosing them in POSIX-compatible wrappers.
> 
> Signed-off-by: Rene Scharfe <l.s.r@web.de>
> ---

This patch is Fine By Me.

Thanks,
Dscho
```
