From 1c3f246198e7b586964554ea3bd2de8b6c5cf823 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ziemowit=20=C5=81=C4=85ski?= <15880281+zlaski@users.noreply.github.com> Date: Tue, 22 Nov 2022 12:29:09 -0800 Subject: [PATCH] pkgconf: Handle spaces correctly when expanding variables Given the following .pc fragment: includedir=/mingw64/include Cflags: -I${includedir} -I${includedir}/taglib Should includedir be assigned the value 'C:/Program\ Files/Git/mingw64/include', the expansion of ${includedir} will be chopped off after the first space: Cflags: -IC:/Program\ With this patch, the expansion is corrected: Cflags: -IC:/Program\ Files/Git/mingw64/include -IC:/Program\ Files/Git/mingw64/include/taglib Create spaces-in-paths.pc --- libpkgconf/argvsplit.c | 13 +++++++++---- tests/lib1/spaces-in-paths.pc | 7 +++++++ 2 files changed, 16 insertions(+), 4 deletions(-) create mode 100644 tests/lib1/spaces-in-paths.pc diff --git a/libpkgconf/argvsplit.c b/libpkgconf/argvsplit.c index aaba858..99cb430 100644 --- a/libpkgconf/argvsplit.c +++ b/libpkgconf/argvsplit.c @@ -80,7 +80,7 @@ pkgconf_argv_split(const char *src, int *argc, char ***argv) if (escaped) { /* POSIX: only \CHAR is special inside a double quote if CHAR is {$, `, ", \, newline}. */ - if (quote == '\"') + if (quote) { if (!(*src_iter == '$' || *src_iter == '`' || *src_iter == '"' || *src_iter == '\\')) *dst_iter++ = '\\'; @@ -88,8 +88,15 @@ pkgconf_argv_split(const char *src, int *argc, char ***argv) *dst_iter++ = *src_iter; } else - *dst_iter++ = *src_iter; + { + /* If we are outside a quoted string/char, an escaped space is usually used to + preserve spaces in file names. */ + if (*src_iter != ' ') + *dst_iter++ = '\\'; + *dst_iter++ = *src_iter; + } + escaped = false; } else if (quote) @@ -118,11 +125,9 @@ pkgconf_argv_split(const char *src, int *argc, char ***argv) } else switch(*src_iter) { -#ifndef _WIN32 case '\\': escaped = true; break; -#endif case '\"': case '\'': diff --git a/tests/lib1/spaces-in-paths.pc b/tests/lib1/spaces-in-paths.pc new file mode 100644 index 0000000..490624e --- /dev/null +++ b/tests/lib1/spaces-in-paths.pc @@ -0,0 +1,7 @@ +prefix=/test\ with\ spaces +includedir=${prefix}/include + +Name: spaces-in-paths +Version: 1 +Description: test package for properly expanding spaces in variables +Cflags: -I${includedir} -I${includedir}/subdir