Skip to contentSkip to conversation

Makefile: escape ' in bin-wrappers rule

PATCH In review Devin Lehmacher 2 participants 2 messages Mar 16, 2017 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Makefile: escape ' in bin-wrappers rule

If the pwd contains a ' escape it as '\'' so that git will not fail while building on a path containing '.

---
 Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Makefile +1 -1
index ed68700ac..5cf5d8537 100644
@@ -2276,7 +2276,7 @@ all:: $(NO_INSTALL)
 bin-wrappers/%: wrap-for-bin.sh
 	@mkdir -p bin-wrappers
 	$(QUIET_GEN)sed -e '1s|#!.*/sh|#!$(SHELL_PATH_SQ)|' \
-	     -e 's|@@BUILD_DIR@@|$(shell pwd)|' \
+	     -e 's|@@BUILD_DIR@@|$(shell pwd | sed -e "s/'/'\\\''/g")|' \
 	     -e 's|@@PROG@@|$(patsubst test-%,t/helper/test-%,$(@F))|' < $< > $@ && \
 	chmod +x $@
 
Signature · 1 line
2.11.0
#1 lore raw
Quoted 2 lines · Devin Lehmacher

If the pwd contains a ' escape it as '\'' so that git will not fail while building on a path containing '.

I think this only fixes half the problem...

Makefile all:: $(NO_INSTALL) · line 2279

Quoted 10 lines
Makefile +1 -1
index ed68700ac..5cf5d8537 100644
@@ -2276,7 +2276,7 @@ all:: $(NO_INSTALL)
 bin-wrappers/%: wrap-for-bin.sh
 	@mkdir -p bin-wrappers
 	$(QUIET_GEN)sed -e '1s|#!.*/sh|#!$(SHELL_PATH_SQ)|' \
-	     -e 's|@@BUILD_DIR@@|$(shell pwd)|' \
+	     -e 's|@@BUILD_DIR@@|$(shell pwd | sed -e "s/'/'\\\''/g")|' \

This escapes the single-quote against the shell run by make. But it does a blind text substitution inside the wrap-for-bin script itself, which means it's subject to another round of expansion. E.g.:

  $ git clone git foo\'git
  $ cd foo\'git
  $ make bin-wrappers/git
  GIT_VERSION = 2.12.0.623.g86ec6c963
  /bin/sh: 4: Syntax error: Unterminated quoted string
  Makefile:2296: recipe for target 'bin-wrappers/git' failed
  make: *** [bin-wrappers/git] Error 2
  $ git am /your/patch
  $ make bin-wrappers/git
  GIT_VERSION = 2.12.0.624.gc9787fbf0
      GEN bin-wrappers/git
  $ bin-wrappers/git
  bin-wrappers/git: 16: bin-wrappers/git: Syntax error: "fi" unexpected

So you'd need an extra layer of quoting. And then to top it all off, passing backslashes through sed requires _another_ layer of quoting. Something like this:

Makefile +6 -1
index 17e381f0c..9c1357a77 100644
@@ -2292,10 +2292,15 @@ test_bindir_programs := $(patsubst %,bin-wrappers/%,$(BINDIR_PROGRAMS_NEED_X) $(
 all:: $(TEST_PROGRAMS) $(test_bindir_programs)
 all:: $(NO_INSTALL)
 
+PWD = $(shell pwd)
+PWD_SQ = $(subst ','\'',$(PWD))
+PWD_SQ_SED = $(subst \,\\,$(PWD_SQ))
+PWD_SQ_SED_SQ = $(subst ','\'',$(PWD_SQ_SED))
+
 bin-wrappers/%: wrap-for-bin.sh
 	@mkdir -p bin-wrappers
 	$(QUIET_GEN)sed -e '1s|#!.*/sh|#!$(SHELL_PATH_SQ)|' \
-	     -e 's|@@BUILD_DIR@@|$(shell pwd)|' \
+	     -e 's|@@BUILD_DIR@@|$(PWD_SQ_SED_SQ)|' \
 	     -e 's|@@PROG@@|$(patsubst test-%,t/helper/test-%,$(@F))|' < $< > $@ && \
 	chmod +x $@
 

That at least gets me as far as building a runnable git. I wouldn't be surprised if there are other gotchas, though. Running "make test" dies in t0060 due to similar substitutions.

I guess if somebody is really determined they can try to fix every spot. But at some point I wonder if the answer is "if it hurts, don't do it".

-Peff