Hex Artifact Content

Artifact 2624af0c94126ec4670e9c7426d40996f0bd202d:


0000: 23 21 20 2f 62 69 6e 2f 62 61 73 68 0a 0a 4c 45  #! /bin/bash..LE
0010: 41 4b 43 48 45 43 4b 46 49 4c 45 3d 22 24 31 22  AKCHECKFILE="$1"
0020: 0a 54 4d 50 46 49 4c 45 3d 22 24 7b 54 4d 50 44  .TMPFILE="${TMPD
0030: 49 52 3a 2d 2f 74 6d 70 7d 2f 6d 61 6c 6c 6f 63  IR:-/tmp}/malloc
0040: 2d 66 72 65 65 2d 63 68 65 63 6b 2d 24 24 24 7b  -free-check-$$${
0050: 52 41 4e 44 4f 4d 7d 24 7b 52 41 4e 44 4f 4d 7d  RANDOM}${RANDOM}
0060: 24 7b 52 41 4e 44 4f 4d 7d 2e 74 6d 70 22 0a 65  ${RANDOM}.tmp".e
0070: 78 70 6f 72 74 20 4c 45 41 4b 43 48 45 43 4b 46  xport LEAKCHECKF
0080: 49 4c 45 20 54 4d 50 46 49 4c 45 0a 0a 69 66 20  ILE TMPFILE..if 
0090: 5b 20 2d 7a 20 22 24 7b 4c 45 41 4b 43 48 45 43  [ -z "${LEAKCHEC
00a0: 4b 46 49 4c 45 7d 22 20 5d 3b 20 74 68 65 6e 0a  KFILE}" ]; then.
00b0: 09 65 63 68 6f 20 22 55 73 61 67 65 3a 20 6c 65  .echo "Usage: le
00c0: 61 6b 63 68 65 63 6b 20 3c 66 69 6c 65 6e 61 6d  akcheck <filenam
00d0: 65 3e 22 20 3e 26 32 0a 09 65 63 68 6f 20 22 20  e>" >&2..echo " 
00e0: 20 20 20 20 20 20 20 20 66 69 6c 65 6e 61 6d 65          filename
00f0: 20 20 20 20 20 20 20 4e 61 6d 65 20 6f 66 20 66         Name of f
0100: 69 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 64  ile containing d
0110: 65 62 75 67 67 69 6e 67 20 6f 75 74 70 75 74 22  ebugging output"
0120: 20 3e 26 32 0a 0a 09 65 78 69 74 20 31 0a 66 69   >&2...exit 1.fi
0130: 0a 0a 65 67 72 65 70 20 27 28 4d 41 4c 4c 4f 43  ..egrep '(MALLOC
0140: 7c 46 52 45 45 7c 52 45 41 4c 4c 4f 43 29 27 20  |FREE|REALLOC)' 
0150: 22 24 7b 4c 45 41 4b 43 48 45 43 4b 46 49 4c 45  "${LEAKCHECKFILE
0160: 7d 22 20 7c 20 73 65 64 20 27 73 40 5e 2e 2a 46  }" | sed 's@^.*F
0170: 52 45 45 28 5c 28 5b 78 30 2d 39 61 2d 66 41 2d  REE(\([x0-9a-fA-
0180: 46 5d 2a 5c 29 29 2e 2a 24 40 66 72 65 65 20 5c  F]*\)).*$@free \
0190: 31 40 3b 73 40 5e 2e 2a 4d 41 4c 4c 4f 43 28 29  1@;s@^.*MALLOC()
01a0: 20 3d 20 40 6d 61 6c 6c 6f 63 20 40 3b 73 40 5e   = @malloc @;s@^
01b0: 2e 2a 52 45 41 4c 4c 4f 43 28 5c 28 5b 78 30 2d  .*REALLOC(\([x0-
01c0: 39 61 2d 66 41 2d 46 5d 2a 5c 29 29 20 3d 20 40  9a-fA-F]*\)) = @
01d0: 72 65 61 6c 6c 6f 63 20 5c 31 20 40 27 20 3e 20  realloc \1 @' > 
01e0: 22 24 7b 54 4d 50 46 49 4c 45 7d 22 0a 0a 63 61  "${TMPFILE}"..ca
01f0: 74 20 22 24 7b 54 4d 50 46 49 4c 45 7d 22 20 7c  t "${TMPFILE}" |
0200: 20 77 68 69 6c 65 20 72 65 61 64 20 6f 70 20 61   while read op a
0210: 64 64 72 20 6e 65 77 61 64 64 72 3b 20 64 6f 0a  ddr newaddr; do.
0220: 09 63 61 73 65 20 22 24 7b 6f 70 7d 22 20 69 6e  .case "${op}" in
0230: 0a 09 09 6d 61 6c 6c 6f 63 29 0a 09 09 09 69 66  ...malloc)....if
0240: 20 5b 20 2d 7a 20 22 24 7b 61 6c 6c 6f 63 6c 69   [ -z "${allocli
0250: 73 74 7d 22 20 5d 3b 20 74 68 65 6e 0a 09 09 09  st}" ]; then....
0260: 09 61 6c 6c 6f 63 6c 69 73 74 3d 22 24 7b 61 64  .alloclist="${ad
0270: 64 72 7d 22 0a 09 09 09 65 6c 73 65 0a 09 09 09  dr}"....else....
0280: 09 61 6c 6c 6f 63 6c 69 73 74 3d 22 24 7b 61 6c  .alloclist="${al
0290: 6c 6f 63 6c 69 73 74 7d 20 24 7b 61 64 64 72 7d  loclist} ${addr}
02a0: 22 0a 09 09 09 66 69 0a 09 09 09 3b 3b 0a 09 09  "....fi....;;...
02b0: 66 72 65 65 29 0a 09 09 09 69 66 20 21 20 65 63  free)....if ! ec
02c0: 68 6f 20 22 20 24 7b 61 6c 6c 6f 63 6c 69 73 74  ho " ${alloclist
02d0: 7d 20 22 20 7c 20 67 72 65 70 20 22 20 24 7b 61  } " | grep " ${a
02e0: 64 64 72 7d 20 22 20 3e 2f 64 65 76 2f 6e 75 6c  ddr} " >/dev/nul
02f0: 6c 3b 20 74 68 65 6e 0a 09 09 09 09 69 66 20 5b  l; then.....if [
0300: 20 2d 7a 20 22 24 7b 61 6c 6c 6f 63 6c 69 73 74   -z "${alloclist
0310: 7d 22 20 5d 3b 20 74 68 65 6e 0a 09 09 09 09 09  }" ]; then......
0320: 61 6c 6c 6f 63 6c 69 73 74 3d 22 21 24 7b 61 64  alloclist="!${ad
0330: 64 72 7d 22 0a 09 09 09 09 65 6c 73 65 0a 09 09  dr}".....else...
0340: 09 09 09 61 6c 6c 6f 63 6c 69 73 74 3d 22 24 7b  ...alloclist="${
0350: 61 6c 6c 6f 63 6c 69 73 74 7d 20 21 24 7b 61 64  alloclist} !${ad
0360: 64 72 7d 22 0a 09 09 09 09 66 69 0a 09 09 09 65  dr}".....fi....e
0370: 6c 73 65 0a 09 09 09 09 61 6c 6c 6f 63 6c 69 73  lse.....alloclis
0380: 74 3d 22 24 28 65 63 68 6f 20 22 20 24 7b 61 6c  t="$(echo " ${al
0390: 6c 6f 63 6c 69 73 74 7d 20 22 20 7c 20 73 65 64  loclist} " | sed
03a0: 20 22 73 40 20 24 7b 61 64 64 72 7d 20 40 20 40   "s@ ${addr} @ @
03b0: 3b 73 40 5e 20 20 2a 40 40 3b 73 40 20 20 2a 5c  ;s@^  *@@;s@  *\
03c0: 24 40 40 22 29 22 0a 09 09 09 66 69 0a 09 09 09  $@@")"....fi....
03d0: 3b 3b 0a 09 09 72 65 61 6c 6c 6f 63 29 0a 09 09  ;;...realloc)...
03e0: 09 61 6c 6c 6f 63 6c 69 73 74 3d 22 24 28 65 63  .alloclist="$(ec
03f0: 68 6f 20 22 20 24 7b 61 6c 6c 6f 63 6c 69 73 74  ho " ${alloclist
0400: 7d 20 22 20 7c 20 73 65 64 20 22 73 40 20 24 7b  } " | sed "s@ ${
0410: 61 64 64 72 7d 20 40 20 24 7b 6e 65 77 61 64 64  addr} @ ${newadd
0420: 72 7d 20 40 3b 73 40 5e 20 20 2a 40 40 3b 73 40  r} @;s@^  *@@;s@
0430: 20 20 2a 5c 24 40 40 22 29 22 0a 09 09 09 3b 3b    *\$@@")"....;;
0440: 0a 09 65 73 61 63 0a 0a 09 65 63 68 6f 20 22 24  ..esac...echo "$
0450: 7b 61 6c 6c 6f 63 6c 69 73 74 7d 22 0a 64 6f 6e  {alloclist}".don
0460: 65 20 7c 20 74 61 69 6c 20 2d 31 20 7c 20 77 68  e | tail -1 | wh
0470: 69 6c 65 20 72 65 61 64 20 6c 65 66 74 6f 76 65  ile read leftove
0480: 72 73 3b 20 64 6f 0a 09 66 6f 72 20 6c 65 66 74  rs; do..for left
0490: 6f 76 65 72 20 69 6e 20 24 7b 6c 65 66 74 6f 76  over in ${leftov
04a0: 65 72 73 7d 3b 20 64 6f 0a 09 09 63 61 73 65 20  ers}; do...case 
04b0: 22 24 7b 6c 65 66 74 6f 76 65 72 7d 22 20 69 6e  "${leftover}" in
04c0: 0a 09 09 09 21 2a 29 0a 09 09 09 09 6c 65 66 74  ....!*).....left
04d0: 6f 76 65 72 3d 22 24 28 65 63 68 6f 20 22 24 7b  over="$(echo "${
04e0: 6c 65 66 74 6f 76 65 72 7d 22 20 7c 20 63 75 74  leftover}" | cut
04f0: 20 2d 63 20 32 2d 29 22 0a 0a 09 09 09 09 65 63   -c 2-)"......ec
0500: 68 6f 20 22 44 6f 75 62 6c 65 20 66 72 65 65 64  ho "Double freed
0510: 20 6f 72 20 6e 65 76 65 72 20 61 6c 6c 6f 63 61   or never alloca
0520: 74 65 64 20 24 7b 6c 65 66 74 6f 76 65 72 7d 3a  ted ${leftover}:
0530: 22 0a 09 09 09 09 67 72 65 70 20 22 24 7b 6c 65  ".....grep "${le
0540: 66 74 6f 76 65 72 7d 22 20 22 24 7b 4c 45 41 4b  ftover}" "${LEAK
0550: 43 48 45 43 4b 46 49 4c 45 7d 22 20 7c 20 73 65  CHECKFILE}" | se
0560: 64 20 27 73 40 5e 40 20 20 20 20 40 27 0a 09 09  d 's@^@    @'...
0570: 09 09 65 63 68 6f 20 27 27 0a 09 09 09 09 3b 3b  ..echo ''.....;;
0580: 0a 09 09 09 2a 29 0a 09 09 09 09 65 63 68 6f 20  ....*).....echo 
0590: 22 55 6e 66 72 65 65 64 20 6d 65 6d 6f 72 79 20  "Unfreed memory 
05a0: 24 7b 6c 65 66 74 6f 76 65 72 7d 3a 22 0a 09 09  ${leftover}:"...
05b0: 09 09 67 72 65 70 20 22 24 7b 6c 65 66 74 6f 76  ..grep "${leftov
05c0: 65 72 7d 22 20 22 24 7b 4c 45 41 4b 43 48 45 43  er}" "${LEAKCHEC
05d0: 4b 46 49 4c 45 7d 22 20 7c 20 73 65 64 20 27 73  KFILE}" | sed 's
05e0: 40 5e 40 20 20 20 20 40 27 0a 09 09 09 09 65 63  @^@    @'.....ec
05f0: 68 6f 20 27 27 0a 09 09 09 09 3b 3b 0a 09 09 65  ho ''.....;;...e
0600: 73 61 63 0a 09 64 6f 6e 65 0a 64 6f 6e 65 0a 0a  sac..done.done..
0610: 72 6d 20 2d 66 20 22 24 7b 54 4d 50 46 49 4c 45  rm -f "${TMPFILE
0620: 7d 22 0a                                         }".