ADDED leakcheck/README.txt Index: leakcheck/README.txt ================================================================== --- /dev/null +++ leakcheck/README.txt @@ -0,0 +1,16 @@ +This script is intended to be used to parse the "debugging" output produced on +stderr when CACKey is compiled with debugging flags enabled. + +Sample usage: + $ ./test 2> cackey-debug.log + Testing libcackey... + PKCS#11 Client Version: 2.30, Library Version 0.0 + ... + Testing libcackey... DONE. Status = 0 + $ ./leakcheck/leakcheck cackey-debug.log + Unfreed memory 0x804d010: + cackey_mutex_create():2017: MALLOC() = 0x804d010 + + +(Note that the leak from cackey_mutex_create() is normal -- there's no safe way +to clean up that mutex) DELETED leakcheck/check-malloc-free.sh Index: leakcheck/check-malloc-free.sh ================================================================== --- leakcheck/check-malloc-free.sh +++ /dev/null @@ -1,53 +0,0 @@ -#! /bin/bash - -TMPFILE="${TMPDIR:-/tmp}/malloc-free-check-$$${RANDOM}${RANDOM}${RANDOM}.tmp" - -egrep '(MALLOC|FREE|REALLOC)' "$@" | sed 's@^.*FREE(\(0x[0-9a-f]*\)).*$@free \1@;s@^.*MALLOC() = @malloc @;s@^.*REALLOC(\(0x[0-9a-f]*\)) = @realloc \1 @' > "${TMPFILE}" - -cat "${TMPFILE}" | while read op addr newaddr; do - case "${op}" in - malloc) - if [ -z "${alloclist}" ]; then - alloclist="${addr}" - else - alloclist="${alloclist} ${addr}" - fi - ;; - free) - if ! echo " ${alloclist} " | grep " ${addr} " >/dev/null; then - if [ -z "${alloclist}" ]; then - alloclist="!${addr}" - else - alloclist="${alloclist} !${addr}" - fi - - continue - fi - alloclist="$(echo " ${alloclist} " | sed "s@ ${addr} @ @;s@^ *@@;s@ *\$@@")" - ;; - realloc) - alloclist="$(echo " ${alloclist} " | sed "s@ ${addr} @ ${newaddr} @;s@^ *@@;s@ *\$@@")" - ;; - esac - - echo "${alloclist}" -done | tail -1 | while read leftovers; do - for leftover in ${leftovers}; do - case "${leftover}" in - !*) - leftover="$(echo "${leftover}" | cut -c 2-)" - - echo "Double freed or never allocated ${leftover}:" - grep "${leftover}" "$@" | sed 's@^@ @' - echo '' - ;; - *) - echo "Unfreed memory ${leftover}:" - grep "${leftover}" "$@" | sed 's@^@ @' - echo '' - ;; - esac - done -done - -rm -f "${TMPFILE}" ADDED leakcheck/leakcheck Index: leakcheck/leakcheck ================================================================== --- /dev/null +++ leakcheck/leakcheck @@ -0,0 +1,53 @@ +#! /bin/bash + +TMPFILE="${TMPDIR:-/tmp}/malloc-free-check-$$${RANDOM}${RANDOM}${RANDOM}.tmp" + +egrep '(MALLOC|FREE|REALLOC)' "$@" | sed 's@^.*FREE(\(0x[0-9a-f]*\)).*$@free \1@;s@^.*MALLOC() = @malloc @;s@^.*REALLOC(\(0x[0-9a-f]*\)) = @realloc \1 @' > "${TMPFILE}" + +cat "${TMPFILE}" | while read op addr newaddr; do + case "${op}" in + malloc) + if [ -z "${alloclist}" ]; then + alloclist="${addr}" + else + alloclist="${alloclist} ${addr}" + fi + ;; + free) + if ! echo " ${alloclist} " | grep " ${addr} " >/dev/null; then + if [ -z "${alloclist}" ]; then + alloclist="!${addr}" + else + alloclist="${alloclist} !${addr}" + fi + + continue + fi + alloclist="$(echo " ${alloclist} " | sed "s@ ${addr} @ @;s@^ *@@;s@ *\$@@")" + ;; + realloc) + alloclist="$(echo " ${alloclist} " | sed "s@ ${addr} @ ${newaddr} @;s@^ *@@;s@ *\$@@")" + ;; + esac + + echo "${alloclist}" +done | tail -1 | while read leftovers; do + for leftover in ${leftovers}; do + case "${leftover}" in + !*) + leftover="$(echo "${leftover}" | cut -c 2-)" + + echo "Double freed or never allocated ${leftover}:" + grep "${leftover}" "$@" | sed 's@^@ @' + echo '' + ;; + *) + echo "Unfreed memory ${leftover}:" + grep "${leftover}" "$@" | sed 's@^@ @' + echo '' + ;; + esac + done +done + +rm -f "${TMPFILE}"