Skip to content
  • Vasily Galkin's avatar
    gspawn, win32: quoted args - escape end backslash · 22e875f7
    Vasily Galkin authored
    According to msdn documentation last backslash(es) of quoted argument
    in a win32 cmdline need to be escaped, since they are
    directly preceding quote in the resulting string:
    https://docs.microsoft.com/en-us/cpp/c-language/parsing-c-command-line-arguments
    
    Glib <=2.58.0 passed children arguments like C:\Program Files\
    without escaping last backslash(es).
    So it had been passed as "C:\Program Files\"
    windows command line parsing treated this as escaped quote,
    and later text was treated as argument continuation instead of separate
    arguments.
    
    Existing implementation wasn't easily adoptable to fix this problem,
    so escaping logic was rewritten.
    Since the resulting length need to be increased due to extra escaping
    it was rewritten too. Now the calculated length assumes that all
    escapable chars would be escaped in a resulting string,
    so the length may be a bit bigger than actually needed,
    since backslashes not preceding quotes are not escaped.
    
    This fixes the glib/tests/spawn-singlethread.c test
    (which introduced testing for special chars to make this problem
    testable).
    The problem itself was found during investigations about fixing
    related #1566
    
    The logic is duplicated in protect_argv_string() and protect_wargv() funcs.
    However there is no single obvious way to get rid of duplication -
    !419 (comment 371483)
    
    So by now adding a note referencing protect_wargv from protect_argv_string,
    the other direction is already referenced.
    22e875f7