#!/bin/sh
# PCP QA Test No. 1736
# mem leak in read_rule() for pmieconf
#
# non-valgrind variant, see qa/1737 for the valgrind variant
#
# Copyright (c) 2026 Ken McDonell.  All Rights Reserved.
#

if [ $# -eq 0 ]
then
    seq=`basename $0`
    echo "QA output created by $seq"
else
    # use $seq from caller, unless not set
    [ -n "$seq" ] || seq=`basename $0`
    echo "QA output created by `basename $0` $*"
fi

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

do_valgrind=false
if [ "$1" = "--valgrind" ]
then
    _check_valgrind
    do_valgrind=true
elif which valgrind >/dev/null 2>&1
then
    [ "$PCPQA_VALGRIND" = both ] || \
        _notrun "valgrind variant qa/1737 will be run"
fi

# test for-some-thing || _notrun No support for some-thing

_cleanup()
{
    cd $here
    $sudo rm -rf $tmp $tmp.*
}

status=0	# success is the default!
trap "_cleanup; exit \$status" 0 1 2 3 15

_filter()
{
    sed \
	-e "s@$tmp@TMP@g" \
	-e "s@/privateTMP@TMP@g" \
    # end
}

_doit()
{
    if $do_valgrind
    then
	_run_valgrind pmieconf -f $tmp.config -r $tmp.badrule
    else
	pmieconf -f $tmp.config -r $tmp.badrule 2>&1
    fi \
    | _filter
}

# real QA test starts here
cat <<'End-of-File' >$tmp.badrule
#pmieconf-rules 1

# bad attribute
rule	foo
	summary = "foo"
	predicate = "hinv.ncpu > 0"
	no_such_attribute = 1
	;
End-of-File
_doit

cat <<'End-of-File' >$tmp.badrule
#pmieconf-rules 1

# premature EOF before end of rule
rule	bar
	summary = "bar"
	help = "stuff that needs cleaning"
End-of-File
_doit

cat <<'End-of-File' >$tmp.badrule
#pmieconf-rules 1

# premature EOF in middle of a value
rule	mumble
	summary = "mumble"
	help = "stuff that needs mumbling"
	predicate = "hinv.ndisk > 0
End-of-File
_doit

# success, all done
exit
