Check-in [709b50760d]
Overview
Comment:Added line numbers to debugging output

Added overloaded debugging strdup to check for leaks

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:709b50760d9414830471584ecbf79669521344b7
User & Date: rkeene on 2010-05-15 00:34:57
Other Links: manifest | tags
Context
2010-05-15
00:39
Updated to censor PIN in debugging output check-in: 0233c7b5fe user: rkeene tags: trunk
00:34
Added line numbers to debugging output

Added overloaded debugging strdup to check for leaks check-in: 709b50760d user: rkeene tags: trunk

00:25
Updated to select correct applet and file for given identity for sign/decrypt

Added more debugging for selecting a key for sign/decrypt check-in: 9fc3b9fa03 user: rkeene tags: trunk

Changes

Modified cackey.c from [36bd79ba26] to [e77001954d].

   155    155   #define GSCIS_AID_CCC                 0xA0, 0x00, 0x00, 0x01, 0x16, 0xDB, 0x00
   156    156   
   157    157   #ifdef CACKEY_DEBUG
   158    158   #  ifdef HAVE_STDIO_H
   159    159   #    include <stdio.h>
   160    160   #  endif
   161    161   
   162         -#  define CACKEY_DEBUG_PRINTF(x...) { fprintf(stderr, "%s(): ", __func__); fprintf(stderr, x); fprintf(stderr, "\n"); }
   163         -#  define CACKEY_DEBUG_PRINTBUF(f, x, y) { unsigned char *TMPBUF; unsigned long idx; TMPBUF = (unsigned char *) (x); fprintf(stderr, "%s(): %s  (%s/%lu = {%02x", __func__, f, #x, (unsigned long) (y), TMPBUF[0]); for (idx = 1; idx < (y); idx++) { fprintf(stderr, ", %02x", TMPBUF[idx]); }; fprintf(stderr, "})\n"); }
   164         -#  define CACKEY_DEBUG_PERROR(x) { fprintf(stderr, "%s(): ", __func__); perror(x); }
          162  +#  define CACKEY_DEBUG_PRINTF(x...) { fprintf(stderr, "%s():%i: ", __func__, __LINE__); fprintf(stderr, x); fprintf(stderr, "\n"); }
          163  +#  define CACKEY_DEBUG_PRINTBUF(f, x, y) { unsigned char *TMPBUF; unsigned long idx; TMPBUF = (unsigned char *) (x); fprintf(stderr, "%s():%i: %s  (%s/%lu = {%02x", __func__, __LINE__, f, #x, (unsigned long) (y), TMPBUF[0]); for (idx = 1; idx < (y); idx++) { fprintf(stderr, ", %02x", TMPBUF[idx]); }; fprintf(stderr, "})\n"); }
          164  +#  define CACKEY_DEBUG_PERROR(x) { fprintf(stderr, "%s():%i: ", __func__, __LINE__); perror(x); }
   165    165   #  define free(x) { CACKEY_DEBUG_PRINTF("FREE(%p) (%s)", x, #x); free(x); }
   166    166   
   167         -static void *CACKEY_DEBUG_FUNC_MALLOC(size_t size, const char *func) {
          167  +static void *CACKEY_DEBUG_FUNC_MALLOC(size_t size, const char *func, int line) {
   168    168   	void *retval;
   169    169   
   170    170   	retval = malloc(size);
   171    171   
   172         -	fprintf(stderr, "%s(): ", func);
          172  +	fprintf(stderr, "%s():%i: ", func, line);
   173    173   	fprintf(stderr, "MALLOC() = %p", retval);
   174    174   	fprintf(stderr, "\n");
   175    175   
   176    176   	return(retval);
   177    177   }
   178    178   
   179         -static void *CACKEY_DEBUG_FUNC_REALLOC(void *ptr, size_t size, const char *func) {
          179  +static void *CACKEY_DEBUG_FUNC_REALLOC(void *ptr, size_t size, const char *func, int line) {
   180    180   	void *retval;
   181    181   
   182    182   	retval = realloc(ptr, size);
   183    183   
   184    184   	if (retval != ptr) {
   185         -		fprintf(stderr, "%s(): ", func);
          185  +		fprintf(stderr, "%s():%i: ", func, line);
   186    186   		fprintf(stderr, "REALLOC(%p) = %p", ptr, retval);
   187    187   		fprintf(stderr, "\n");
   188    188   	}
   189    189   
          190  +	return(retval);
          191  +}
          192  +
          193  +static char *CACKEY_DEBUG_FUNC_STRDUP(const char *ptr, const char *func, int line) {
          194  +	char *retval;
          195  +
          196  +	retval = strdup(ptr);
          197  +
          198  +	fprintf(stderr, "%s():%i: ", func, line);
          199  +	fprintf(stderr, "STRDUP_MALLOC() = %p", retval);
          200  +	fprintf(stderr, "\n");
          201  +
   190    202   	return(retval);
   191    203   }
   192    204   
   193    205   static const char *CACKEY_DEBUG_FUNC_TAG_TO_STR(unsigned char tag) {
   194    206   	switch (tag) {
   195    207   		case GSCIS_TAG_CARDID:
   196    208   			return("GSCIS_TAG_CARDID");
................................................................................
   422    434   		case 0x07:
   423    435   			return("CACKEY_TLV_APP_GENERIC | CACKEY_TLV_APP_SKI | CACKEY_TLV_APP_PKI");
   424    436   	}
   425    437   
   426    438   	return("INVALID");
   427    439   }
   428    440   
   429         -#  define malloc(x) CACKEY_DEBUG_FUNC_MALLOC(x, __func__)
   430         -#  define realloc(x, y) CACKEY_DEBUG_FUNC_REALLOC(x, y, __func__)
          441  +#  define malloc(x) CACKEY_DEBUG_FUNC_MALLOC(x, __func__, __LINE__)
          442  +#  define realloc(x, y) CACKEY_DEBUG_FUNC_REALLOC(x, y, __func__, __LINE__)
          443  +#  ifdef strdup
          444  +#    undef strdup
          445  +#  endif
          446  +#  define strdup(x) CACKEY_DEBUG_FUNC_STRDUP(x, __func__, __LINE__)
   431    447   #else
   432    448   #  define CACKEY_DEBUG_PRINTF(x...) /**/
   433    449   #  define CACKEY_DEBUG_PRINTBUF(f, x, y) /**/
   434    450   #  define CACKEY_DEBUG_PERROR(x) /**/
   435    451   #  define CACKEY_DEBUG_FUNC_TAG_TO_STR(x) "DEBUG_DISABLED"
   436    452   #  define CACKEY_DEBUG_FUNC_SCARDERR_TO_STR(x) "DEBUG_DISABLED"
   437    453   #  define CACKEY_DEBUG_FUNC_OBJID_TO_STR(x) "DEBUG_DISABLED"

Added leakcheck/check-malloc-free.sh version [ebd6df1f69].

            1  +#! /bin/bash
            2  +
            3  +TMPFILE="${TMPDIR:-/tmp}/malloc-free-check-$$${RANDOM}${RANDOM}${RANDOM}.tmp"
            4  +
            5  +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}"
            6  +
            7  +cat "${TMPFILE}" | while read op addr newaddr; do
            8  +	case "${op}" in
            9  +		malloc)
           10  +			if [ -z "${alloclist}" ]; then
           11  +				alloclist="${addr}"
           12  +			else
           13  +				alloclist="${alloclist} ${addr}"
           14  +			fi
           15  +			;;
           16  +		free)
           17  +			if ! echo " ${alloclist} " | grep " ${addr} " >/dev/null; then
           18  +				if [ -z "${alloclist}" ]; then
           19  +					alloclist="!${addr}"
           20  +				else
           21  +					alloclist="${alloclist} !${addr}"
           22  +				fi
           23  +
           24  +				continue
           25  +			fi
           26  +			alloclist="$(echo " ${alloclist} " | sed "s@ ${addr} @ @;s@^  *@@;s@  *\$@@")"
           27  +			;;
           28  +		realloc)
           29  +			alloclist="$(echo " ${alloclist} " | sed "s@ ${addr} @ ${newaddr} @;s@^  *@@;s@  *\$@@")"
           30  +			;;
           31  +	esac
           32  +
           33  +	echo "${alloclist}"
           34  +done | tail -1 | while read leftovers; do
           35  +	for leftover in ${leftovers}; do
           36  +		case "${leftover}" in
           37  +			!*)
           38  +				leftover="$(echo "${leftover}" | cut -c 2-)"
           39  +
           40  +				echo "Double freed or never allocated ${leftover}:"
           41  +				grep "${leftover}" "$@" | sed 's@^@    @'
           42  +				echo ''
           43  +				;;
           44  +			*)
           45  +				echo "Unfreed memory ${leftover}:"
           46  +				grep "${leftover}" "$@" | sed 's@^@    @'
           47  +				echo ''
           48  +				;;
           49  +		esac
           50  +	done
           51  +done
           52  +
           53  +rm -f "${TMPFILE}"