Changeset 122042


Ignore:
Timestamp:
Jul 13, 2014, 12:55:29 PM (10 years ago)
Author:
cal@…
Message:

base: macports1.0: provide shell escaping function, use shell escaping for the selfupdate arguments in an attempt to fix #43875

Location:
trunk/base/src/macports1.0
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/base/src/macports1.0/macports.tcl

    r120382 r122042  
    35403540            ui_debug "Permissions OK"
    35413541
    3542             set configure_args "--prefix=$prefix --with-install-user=$owner --with-install-group=$group --with-directory-mode=$perms"
     3542            set configure_args "--prefix=[macports::shellescape $prefix] --with-install-user=[macports::shellescape $owner] --with-install-group=[macports::shellescape $group] --with-directory-mode=[macports::shellescape $perms]"
    35433543            # too many users have an incompatible readline in /usr/local, see ticket #10651
    35443544            if {$tcl_platform(os) ne {Darwin} || $prefix eq {/usr/local}
     
    50035003    return $archive_sites_conf_values
    50045004}
     5005
     5006##
     5007# Escape a string for use in a POSIX shell, e.g., when passing it to the \c system Pextlib extension. This is necessary
     5008# to handle cases such as group names with backslashes correctly. See #43875 for an example of a problem caused by
     5009# missing quotes.
     5010#
     5011# @param arg The argument that should be escaped for use in a POSIX shell
     5012# @return A quoted version of the argument
     5013proc macports::shellescape {arg} {
     5014    set mapping {}
     5015    # Replace each backslash by a double backslash. Apparently Bash treats Backslashes in single-quoted strings
     5016    # differently depending on whether is was invoked as sh or bash: echo 'using \backslashes' preserves the backslash
     5017    # in bash mode, but interprets it in sh mode. Since the `system' command uses sh, escape backslashes.
     5018    lappend mapping "\\" "\\\\"
     5019    # Replace each single quote with a single quote (closing the currently open string), an escaped single quote \'
     5020    # (additional backslash needed to escape the backslash in Tcl), and another single quote (opening a new quoted
     5021    # string).
     5022    lappend mapping "'" "'\\''"
     5023
     5024    # Add a single quote at the start, escape all single quotes in the argument, and add a single quote at the end
     5025    return "'[string map $mapping $arg]'"
     5026}
  • trunk/base/src/macports1.0/tests/macports.test

    r120187 r122042  
    916916
    917917
     918set shellescapeTests [list \
     919    "using \\backslashes" \
     920    " spaces " \
     921    "and        tabs" \
     922    "quotes need to be \"supported\", too" \
     923    "… and not only 'double-quotes'" \
     924    "other meta chars such as \$dollar," \
     925    "!bang, ;semicolon, :colon," \
     926    "\$(subshells) and similar must be kept" \
     927    ">redirects <& must be ignored as well as ampersands &"]
     928test shellescaping {
     929    Check whether shell escaping using macports::shellescape works correctly when passed to Pextlib's system extension.
     930} -setup {
     931    set outputfile "shellescapetestoutput.txt"
     932    makeFile "" $outputfile
     933
     934} -body {
     935    set first "yes"
     936    foreach test $shellescapeTests {
     937        if {$first eq "yes"} {
     938            system "echo [macports::shellescape $test]  >$outputfile"
     939            set first "no"
     940        } else {
     941            system "echo [macports::shellescape $test] >>$outputfile"
     942        }
     943    }
     944
     945    set fd [open $outputfile r]
     946    set output [read -nonewline $fd]
     947    close $fd
     948    return $output
     949} -cleanup {
     950    removeFile $outputfile
     951} -result [join $shellescapeTests "\n"]
     952
    918953cleanupTests
Note: See TracChangeset for help on using the changeset viewer.