Merge: More escaping
authorJean Privat <jean@pryen.org>
Thu, 13 Nov 2014 05:10:39 +0000 (00:10 -0500)
committerJean Privat <jean@pryen.org>
Thu, 13 Nov 2014 05:10:39 +0000 (00:10 -0500)
compiler is more robust.

The only issue seems to be the Makefile since there is no way to have an arbitrary named goal.

Pull-Request: #899
Reviewed-by: Lucas Bajolet <r4pass@hotmail.com>

1  2 
tests/tests.sh

diff --combined tests/tests.sh
@@@ -27,13 -27,13 +27,13 @@@ unset NIT_DI
  
  # Get the first Java lib available
  shopt -s nullglob
 -paths=`echo /usr/lib/jvm/*/`
 -paths=($paths)        
 -JAVA_HOME=${paths[0]}
 +JAVA_HOME=$(dirname $(dirname $(readlink -f $(which javac))))
  
  paths=`echo $JAVA_HOME/jre/lib/*/{client,server}/`
  paths=($paths)        
  JNI_LIB_PATH=${paths[0]}
 +echo $JAVA_HOME
 +echo $JNI_LIB_PATH
  shopt -u nullglob
  
  outdir="out"
@@@ -65,7 -65,7 +65,7 @@@ saferun(
        local o=
        local a=
        while [ $stop = false ]; do
-               case $1 in
+               case "$1" in
                        -o) o="$2"; shift; shift;;
                        -a) a="-a"; shift;;
                        *) stop=true
@@@ -129,8 -129,8 +129,8 @@@ function compare_to_result(
        local pattern="$1"
        local sav="$2"
        if [ ! -r "$sav" ]; then return 0; fi
-       test "`cat "$sav"`" = "UNDEFINED" && return 1
-       diff -u "$sav" "$outdir/$pattern.res" > "$outdir/$pattern.diff.sav.log"
+       test "`cat -- "$sav"`" = "UNDEFINED" && return 1
+       diff -u -- "$sav" "$outdir/$pattern.res" > "$outdir/$pattern.diff.sav.log"
        if [ "$?" == 0 ]; then
                return 1
        fi
        fi
  }
  
+ function xmlesc()
+ {
+       sed 's/&/\&amp;/g; s/</\&lt;/g; s/>/\&gt;/g; s/"/\&quot;/g; s/'"'"'/\&#39;/g'<<EOF
+ $*
+ EOF
+ }
  # As argument: the pattern used for the file
  function process_result()
  {
        OLD=""
        LIST=""
        FIRST=""
-       echo >>$xml "<testcase classname='$pack' name='$description' time='`cat $outdir/$pattern.time.out`' `timestamp`>"
+       echo >>$xml "<testcase classname='`xmlesc "$pack"`' name='`xmlesc "$description"`' time='`cat -- "$outdir/$pattern.time.out"`' `timestamp`>"
        #for sav in "sav/$engine/fixme/$pattern.res" "sav/$engine/$pattern.res" "sav/fixme/$pattern.res" "sav/$pattern.res" "sav/$pattern.sav"; do
        for savdir in $savdirs; do
                sav=$savdir/fixme/$pattern.res
        if [ -n "$SAV" ]; then
                if [ -n "$OLD" ]; then
                        echo "[*ok*] $outdir/$pattern.res $SAV - but $OLD remains!"
-                       echo >>$xml "<error message='ok $outdir/$pattern.res - but $OLD remains'/>"
+                       echo >>$xml "<error message='`xmlesc "ok $outdir/$pattern.res - but $OLD remains"`'/>"
                        remains="$remains $OLD"
                else
                        echo "[ok] $outdir/$pattern.res $SAV"
        elif [ -n "$FIXME" ]; then
                if [ -n "$OLD" ]; then
                        echo "[*fixme*] $outdir/$pattern.res $FIXME - but $OLD remains!"
-                       echo >>$xml "<error message='ok $outdir/$pattern.res - but $OLD remains'/>"
+                       echo >>$xml "<error message='`xmlesc "ok $outdir/$pattern.res - but $OLD remains"`'/>"
                        remains="$remains $OLD"
                else
                        echo "[fixme] $outdir/$pattern.res $FIXME"
                todos="$todos $pattern"
        elif [ -n "$SOSO" ]; then
                echo "[======= soso $outdir/$pattern.res $SOSO =======]"
-               echo >>$xml "<error message='soso $outdir/$pattern.res $SOSO'/>"
+               echo >>$xml "<error message='`xmlesc "soso $outdir/$pattern.res $SOSO"`'/>"
                echo >>$xml "<system-out><![CDATA["
-               cat -v $outdir/$pattern.diff.sav.log | head >>$xml -n 50
+               cat -v -- "$outdir/$pattern.diff.sav.log" | head >>$xml -n 50
                echo >>$xml "]]></system-out>"
                nok="$nok $pattern"
                echo "$ii" >> "$ERRLIST"
        elif [ -n "$SOSOF" ]; then
                echo "[======= fixme soso $outdir/$pattern.res $SOSOF =======]"
-               echo >>$xml "<error message='soso $outdir/$pattern.res $SOSO'/>"
+               echo >>$xml "<error message='`xmlesc "soso $outdir/$pattern.res $SOSO"`'/>"
                echo >>$xml "<system-out><![CDATA["
-               cat -v $outdir/$pattern.diff.sav.log | head >>$xml -n 50
+               cat -v  -- "$outdir/$pattern.diff.sav.log" | head >>$xml -n 50
                echo >>$xml "]]></system-out>"
                nok="$nok $pattern"
                echo "$ii" >> "$ERRLIST"
        elif [ -n "$NSAV" ]; then
                echo "[======= fail $outdir/$pattern.res $NSAV =======]"
-               echo >>$xml "<error message='fail $outdir/$pattern.res $NSAV'/>"
+               echo >>$xml "<error message='`xmlesc "fail $outdir/$pattern.res $NSAV"`'/>"
                echo >>$xml "<system-out><![CDATA["
-               cat -v $outdir/$pattern.diff.sav.log | head >>$xml -n 50
+               cat -v -- "$outdir/$pattern.diff.sav.log" | head >>$xml -n 50
                echo >>$xml "]]></system-out>"
                nok="$nok $pattern"
                echo "$ii" >> "$ERRLIST"
        elif [ -n "$NFIXME" ]; then
                echo "[======= changed $outdir/$pattern.res $NFIXME ======]"
-               echo >>$xml "<error message='changed $outdir/$pattern.res $NFIXME'/>"
+               echo >>$xml "<error message='`xmlesc "changed $outdir/$pattern.res $NFIXME"`'/>"
                echo >>$xml "<system-out><![CDATA["
-               cat -v $outdir/$pattern.diff.sav.log | head >>$xml -n 50
+               cat -v -- "$outdir/$pattern.diff.sav.log" | head >>$xml -n 50
                echo >>$xml "]]></system-out>"
                nok="$nok $pattern"
                echo "$ii" >> "$ERRLIST"
-       elif [ -s $outdir/$pattern.res ]; then
+       elif [ -s "$outdir/$pattern.res" ]; then
                echo "[=== no sav ===] $outdir/$pattern.res is not empty"
                echo >>$xml "<error message='no sav and not empty'/>"
                echo >>$xml "<system-out><![CDATA["
-               cat -v >>$xml $outdir/$pattern.res
+               cat -v >>$xml -- "$outdir/$pattern.res"
                echo >>$xml "]]></system-out>"
                nos="$nos $pattern"
                echo "$ii" >> "$ERRLIST"
                echo "[0k] $outdir/$pattern.res is empty"
                ok="$ok $pattern"
        fi
-       if test -s $outdir/$pattern.cmp.err; then
+       if test -s "$outdir/$pattern.cmp.err"; then
                echo >>$xml "<system-err><![CDATA["
-               cat -v >>$xml $outdir/$pattern.cmp.err
+               cat -v >>$xml -- "$outdir/$pattern.cmp.err"
                echo >>$xml "]]></system-err>"
        fi
        echo >>$xml "</testcase>"
@@@ -303,12 -310,12 +310,12 @@@ need_skip(
        test "$noskip" = true && return 1
        if echo "$1" | grep -f "$engine.skip" >/dev/null 2>&1; then
                echo "=> $2: [skip]"
-               echo >>$xml "<testcase classname='$3' name='$2' `timestamp`><skipped/></testcase>"
+               echo >>$xml "<testcase classname='`xmlesc "$3"`' name='`xmlesc "$2"`' `timestamp`><skipped/></testcase>"
                return 0
        fi
        if test -n "$isinterpret" && echo "$1" | grep -f "exec.skip" >/dev/null 2>&1; then
                echo "=> $2: [skip exec]"
-               echo >>$xml "<testcase classname='$3' name='$2' `timestamp`><skipped/></testcase>"
+               echo >>$xml "<testcase classname='`xmlesc "$3"`' name='`xmlesc "$2"`' `timestamp`><skipped/></testcase>"
                return 0
        fi
        return 1
@@@ -373,27 -380,22 +380,27 @@@ case $engine i
                engine=nitg-s;
                enginebinname=nitg;
                OPT="--separate $OPT --compile-dir $compdir"
 +              savdirs="sav/nitg-common/"
                ;;
        nitg-s)
                enginebinname=nitg;
                OPT="--separate $OPT --compile-dir $compdir"
 +              savdirs="sav/nitg-common/"
                ;;
        nitg-e)
                enginebinname=nitg;
                OPT="--erasure $OPT --compile-dir $compdir"
 +              savdirs="sav/nitg-common/"
                ;;
        nitg-sg)
                enginebinname=nitg;
                OPT="--semi-global $OPT --compile-dir $compdir"
 +              savdirs="sav/nitg-common/"
                ;;
        nitg-g)
                enginebinname=nitg;
                OPT="--global $OPT --compile-dir $compdir"
 +              savdirs="sav/nitg-common/"
                ;;
        nit)
                engine=niti
  echo >$xml "<testsuites><testsuite>"
  
  for ii in "$@"; do
-       if [ ! -f $ii ]; then
+       if [ ! -f "$ii" ]; then
                echo "File '$ii' does not exist."
                continue
        fi
-       f=`basename "$ii" .nit`
+       f=`basename -- "$ii" .nit`
  
        pack="tests.${engine}".`echo $ii | perl -p -e 's|^../([^/]*)/([a-zA-Z_]*).*|\1.\2| || s|^([a-zA-Z]*)[^_]*_([a-zA-Z]*).*|\1.\2| || s|\W*([a-zA-Z_]*).*|\1|'`
  
        # Sould we skip the file for this engine?
-       need_skip $f $f $pack && continue
+       need_skip "$f" "$f" "$pack" && continue
  
        tmp=${ii/../AA}
        if [ "x$tmp" = "x$ii" ]; then
                includes="-I alt"
        fi
  
-       for i in "$ii" `./alterner.pl --start '#' --altsep '_' $ii`; do
-               bf=`basename $i .nit`
+       for i in "$ii" `./alterner.pl --start '#' --altsep '_' -- "$ii"`; do
+               bf=`basename -- "$i" .nit`
                ff="$outdir/$bf"
  
                # Sould we skip the alternative for this engine?
-               need_skip $bf $bf $pack && continue
+               need_skip "$bf" "$bf" "$pack" && continue
  
                echo -n "=> $bf: "
  
  
                if [ -n "$isinterpret" ]; then
                        cat > "$ff.bin" <<END
- exec $NITC --no-color $OPT $includes -- "$i" "\$@"
+ exec $NITC --no-color $OPT $includes -- $(printf '%q' "$i") "\$@"
  END
                        chmod +x "$ff.bin"
                        > "$ff.cmp.err"
                                saferun -o "$ff.time.out" $NITC --no-color $OPT -o "$ffout" $includes $nocc "$i" 2> "$ff.cmp.err" > "$ff.compile.log"
                        ERR=$?
                        if [ "x$verbose" = "xtrue" ]; then
-                               cat "$ff.compile.log"
-                               cat >&2 "$ff.cmp.err"
+                               cat -- "$ff.compile.log"
+                               cat >&2 -- "$ff.cmp.err"
                        fi
                fi
                if [ "$engine" = "emscripten" ]; then
                        chmod +x "$ff.bin"
                        if grep "Fatal Error: more than one primitive class" "$ff.compile.log" > /dev/null; then
                                echo " [skip] do no not imports kernel"
-                               echo >>$xml "<testcase classname='$pack' name='$bf' `timestamp`><skipped/></testcase>"
+                               echo >>$xml "<testcase classname='`xmlesc "$pack"`' name='`xmlesc "$bf"`' `timestamp`><skipped/></testcase>"
                                continue
                        fi
                fi
                if [ "$ERR" != 0 ]; then
                        echo -n "! "
-                       cat "$ff.compile.log" "$ff.cmp.err" > "$ff.res"
-                       process_result $bf $bf $pack
+                       cat -- "$ff.compile.log" "$ff.cmp.err" > "$ff.res"
+                       process_result "$bf" "$bf" "$pack"
                elif [ -n "$nocc" ]; then
                        # not compiled
                        echo -n "nocc "
                        > "$ff.res"
-                       process_result $bf $bf $pack
+                       process_result "$bf" "$bf" "$pack"
                elif [ -x "$ff.bin" ]; then
                        if skip_exec "$bf"; then
                                # No exec
                                > "$ff.res"
-                               process_result $bf $bf $pack
+                               process_result "$bf" "$bf" "$pack"
                                break
                        fi
                        echo -n ". "
                        fi
                        NIT_NO_STACK=1 LD_LIBRARY_PATH=$JNI_LIB_PATH \
                                saferun -a -o "$ff.time.out" "$ff.bin" $args < "$inputs" > "$ff.res" 2>"$ff.err"
-                       mv $ff.time.out $ff.times.out
-                       awk '{ SUM += $1} END { print SUM }' $ff.times.out > $ff.time.out
+                       mv "$ff.time.out" "$ff.times.out"
+                       awk '{ SUM += $1} END { print SUM }' "$ff.times.out" > "$ff.time.out"
  
                        if [ "x$verbose" = "xtrue" ]; then
-                               cat "$ff.res"
-                               cat >&2 "$ff.err"
+                               cat -- "$ff.res"
+                               cat >&2 -- "$ff.err"
                        fi
                        if [ -f "$ff.write" ]; then
-                               cat "$ff.write" >> "$ff.res"
+                               cat -- "$ff.write" >> "$ff.res"
                        elif [ -d "$ff.write" ]; then
-                               LANG=C /bin/ls -F $ff.write >> "$ff.res"
+                               LANG=C /bin/ls -F "$ff.write" >> "$ff.res"
                        fi
-                       cp "$ff.res"  "$ff.res2"
-                       cat "$ff.cmp.err" "$ff.err" "$ff.res2" > "$ff.res"
-                       process_result $bf $bf $pack
+                       cp -- "$ff.res"  "$ff.res2"
+                       cat -- "$ff.cmp.err" "$ff.err" "$ff.res2" > "$ff.res"
+                       process_result "$bf" "$bf" "$pack"
  
                        if [ -f "$f.args" ]; then
                                fargs=$f.args
                                        name="$bf args $cptr"
  
                                        # Sould we skip the input for this engine?
-                                       need_skip $bff "  $name" $pack && continue
+                                       need_skip "$bff" "  $name" "$pack" && continue
  
                                        # use a specific inputs file, if required
                                        if [ -f "$bff.inputs" ]; then
                                                ffinputs="$bff.inputs"
                                        else
-                                               ffinputs=$inputs
+                                               ffinputs="$inputs"
                                        fi
  
                                        rm -rf "$fff.res" "$fff.err" "$fff.write" 2> /dev/null
                                        chmod +x "$fff.bin"
                                        WRITE="$fff.write" saferun -o "$fff.time.out" sh -c "NIT_NO_STACK=1 $fff.bin < $ffinputs > $fff.res 2>$fff.err"
                                        if [ "x$verbose" = "xtrue" ]; then
-                                               cat "$fff.res"
-                                               cat >&2 "$fff.err"
+                                               cat -- "$fff.res"
+                                               cat >&2 -- "$fff.err"
                                        fi
                                        if [ -f "$fff.write" ]; then
-                                               cat "$fff.write" >> "$fff.res"
+                                               cat -- "$fff.write" >> "$fff.res"
                                        elif [ -d "$fff.write" ]; then
-                                               LANG=C /bin/ls -F $fff.write >> "$fff.res"
+                                               LANG=C /bin/ls -F -- "$fff.write" >> "$fff.res"
                                        fi
                                        if [ -s "$fff.err" ]; then
-                                               cp "$fff.res"  "$fff.res2"
-                                               cat "$fff.err" "$fff.res2" > "$fff.res"
+                                               cp -- "$fff.res"  "$fff.res2"
+                                               cat -- "$fff.err" "$fff.res2" > "$fff.res"
                                        fi
-                                       process_result $bff "  $name" $pack
-                               done < $fargs
+                                       process_result "$bff" "  $name" "$pack"
+                               done < "$fargs"
                        fi
                elif [ -f "$ff.bin" ]; then
                        #Not executable (platform?)"
                        > "$ff.res"
-                       process_result $bf "$bf" $pack
+                       process_result "$bf" "$bf" "$pack"
                else
                        echo -n "! "
-                       cat "$ff.cmp.err" > "$ff.res"
+                       cat -- "$ff.cmp.err" > "$ff.res"
                        echo "Compilation error" > "$ff.res"
-                       process_result $bf "$bf" $pack
+                       process_result "$bf" "$bf" "$pack"
                fi
        done
  done