Hex Artifact Content

Artifact ebd6df1f69c757b3364b5ce3e38fbdee82db499e:


0000: 23 21 20 2f 62 69 6e 2f 62 61 73 68 0a 0a 54 4d  #! /bin/bash..TM
0010: 50 46 49 4c 45 3d 22 24 7b 54 4d 50 44 49 52 3a  PFILE="${TMPDIR:
0020: 2d 2f 74 6d 70 7d 2f 6d 61 6c 6c 6f 63 2d 66 72  -/tmp}/malloc-fr
0030: 65 65 2d 63 68 65 63 6b 2d 24 24 24 7b 52 41 4e  ee-check-$$${RAN
0040: 44 4f 4d 7d 24 7b 52 41 4e 44 4f 4d 7d 24 7b 52  DOM}${RANDOM}${R
0050: 41 4e 44 4f 4d 7d 2e 74 6d 70 22 0a 0a 65 67 72  ANDOM}.tmp"..egr
0060: 65 70 20 27 28 4d 41 4c 4c 4f 43 7c 46 52 45 45  ep '(MALLOC|FREE
0070: 7c 52 45 41 4c 4c 4f 43 29 27 20 22 24 40 22 20  |REALLOC)' "$@" 
0080: 7c 20 73 65 64 20 27 73 40 5e 2e 2a 46 52 45 45  | sed 's@^.*FREE
0090: 28 5c 28 30 78 5b 30 2d 39 61 2d 66 5d 2a 5c 29  (\(0x[0-9a-f]*\)
00a0: 29 2e 2a 24 40 66 72 65 65 20 5c 31 40 3b 73 40  ).*$@free \1@;s@
00b0: 5e 2e 2a 4d 41 4c 4c 4f 43 28 29 20 3d 20 40 6d  ^.*MALLOC() = @m
00c0: 61 6c 6c 6f 63 20 40 3b 73 40 5e 2e 2a 52 45 41  alloc @;s@^.*REA
00d0: 4c 4c 4f 43 28 5c 28 30 78 5b 30 2d 39 61 2d 66  LLOC(\(0x[0-9a-f
00e0: 5d 2a 5c 29 29 20 3d 20 40 72 65 61 6c 6c 6f 63  ]*\)) = @realloc
00f0: 20 5c 31 20 40 27 20 3e 20 22 24 7b 54 4d 50 46   \1 @' > "${TMPF
0100: 49 4c 45 7d 22 0a 0a 63 61 74 20 22 24 7b 54 4d  ILE}"..cat "${TM
0110: 50 46 49 4c 45 7d 22 20 7c 20 77 68 69 6c 65 20  PFILE}" | while 
0120: 72 65 61 64 20 6f 70 20 61 64 64 72 20 6e 65 77  read op addr new
0130: 61 64 64 72 3b 20 64 6f 0a 09 63 61 73 65 20 22  addr; do..case "
0140: 24 7b 6f 70 7d 22 20 69 6e 0a 09 09 6d 61 6c 6c  ${op}" in...mall
0150: 6f 63 29 0a 09 09 09 69 66 20 5b 20 2d 7a 20 22  oc)....if [ -z "
0160: 24 7b 61 6c 6c 6f 63 6c 69 73 74 7d 22 20 5d 3b  ${alloclist}" ];
0170: 20 74 68 65 6e 0a 09 09 09 09 61 6c 6c 6f 63 6c   then.....allocl
0180: 69 73 74 3d 22 24 7b 61 64 64 72 7d 22 0a 09 09  ist="${addr}"...
0190: 09 65 6c 73 65 0a 09 09 09 09 61 6c 6c 6f 63 6c  .else.....allocl
01a0: 69 73 74 3d 22 24 7b 61 6c 6c 6f 63 6c 69 73 74  ist="${alloclist
01b0: 7d 20 24 7b 61 64 64 72 7d 22 0a 09 09 09 66 69  } ${addr}"....fi
01c0: 0a 09 09 09 3b 3b 0a 09 09 66 72 65 65 29 0a 09  ....;;...free)..
01d0: 09 09 69 66 20 21 20 65 63 68 6f 20 22 20 24 7b  ..if ! echo " ${
01e0: 61 6c 6c 6f 63 6c 69 73 74 7d 20 22 20 7c 20 67  alloclist} " | g
01f0: 72 65 70 20 22 20 24 7b 61 64 64 72 7d 20 22 20  rep " ${addr} " 
0200: 3e 2f 64 65 76 2f 6e 75 6c 6c 3b 20 74 68 65 6e  >/dev/null; then
0210: 0a 09 09 09 09 69 66 20 5b 20 2d 7a 20 22 24 7b  .....if [ -z "${
0220: 61 6c 6c 6f 63 6c 69 73 74 7d 22 20 5d 3b 20 74  alloclist}" ]; t
0230: 68 65 6e 0a 09 09 09 09 09 61 6c 6c 6f 63 6c 69  hen......allocli
0240: 73 74 3d 22 21 24 7b 61 64 64 72 7d 22 0a 09 09  st="!${addr}"...
0250: 09 09 65 6c 73 65 0a 09 09 09 09 09 61 6c 6c 6f  ..else......allo
0260: 63 6c 69 73 74 3d 22 24 7b 61 6c 6c 6f 63 6c 69  clist="${allocli
0270: 73 74 7d 20 21 24 7b 61 64 64 72 7d 22 0a 09 09  st} !${addr}"...
0280: 09 09 66 69 0a 0a 09 09 09 09 63 6f 6e 74 69 6e  ..fi......contin
0290: 75 65 0a 09 09 09 66 69 0a 09 09 09 61 6c 6c 6f  ue....fi....allo
02a0: 63 6c 69 73 74 3d 22 24 28 65 63 68 6f 20 22 20  clist="$(echo " 
02b0: 24 7b 61 6c 6c 6f 63 6c 69 73 74 7d 20 22 20 7c  ${alloclist} " |
02c0: 20 73 65 64 20 22 73 40 20 24 7b 61 64 64 72 7d   sed "s@ ${addr}
02d0: 20 40 20 40 3b 73 40 5e 20 20 2a 40 40 3b 73 40   @ @;s@^  *@@;s@
02e0: 20 20 2a 5c 24 40 40 22 29 22 0a 09 09 09 3b 3b    *\$@@")"....;;
02f0: 0a 09 09 72 65 61 6c 6c 6f 63 29 0a 09 09 09 61  ...realloc)....a
0300: 6c 6c 6f 63 6c 69 73 74 3d 22 24 28 65 63 68 6f  lloclist="$(echo
0310: 20 22 20 24 7b 61 6c 6c 6f 63 6c 69 73 74 7d 20   " ${alloclist} 
0320: 22 20 7c 20 73 65 64 20 22 73 40 20 24 7b 61 64  " | sed "s@ ${ad
0330: 64 72 7d 20 40 20 24 7b 6e 65 77 61 64 64 72 7d  dr} @ ${newaddr}
0340: 20 40 3b 73 40 5e 20 20 2a 40 40 3b 73 40 20 20   @;s@^  *@@;s@  
0350: 2a 5c 24 40 40 22 29 22 0a 09 09 09 3b 3b 0a 09  *\$@@")"....;;..
0360: 65 73 61 63 0a 0a 09 65 63 68 6f 20 22 24 7b 61  esac...echo "${a
0370: 6c 6c 6f 63 6c 69 73 74 7d 22 0a 64 6f 6e 65 20  lloclist}".done 
0380: 7c 20 74 61 69 6c 20 2d 31 20 7c 20 77 68 69 6c  | tail -1 | whil
0390: 65 20 72 65 61 64 20 6c 65 66 74 6f 76 65 72 73  e read leftovers
03a0: 3b 20 64 6f 0a 09 66 6f 72 20 6c 65 66 74 6f 76  ; do..for leftov
03b0: 65 72 20 69 6e 20 24 7b 6c 65 66 74 6f 76 65 72  er in ${leftover
03c0: 73 7d 3b 20 64 6f 0a 09 09 63 61 73 65 20 22 24  s}; do...case "$
03d0: 7b 6c 65 66 74 6f 76 65 72 7d 22 20 69 6e 0a 09  {leftover}" in..
03e0: 09 09 21 2a 29 0a 09 09 09 09 6c 65 66 74 6f 76  ..!*).....leftov
03f0: 65 72 3d 22 24 28 65 63 68 6f 20 22 24 7b 6c 65  er="$(echo "${le
0400: 66 74 6f 76 65 72 7d 22 20 7c 20 63 75 74 20 2d  ftover}" | cut -
0410: 63 20 32 2d 29 22 0a 0a 09 09 09 09 65 63 68 6f  c 2-)"......echo
0420: 20 22 44 6f 75 62 6c 65 20 66 72 65 65 64 20 6f   "Double freed o
0430: 72 20 6e 65 76 65 72 20 61 6c 6c 6f 63 61 74 65  r never allocate
0440: 64 20 24 7b 6c 65 66 74 6f 76 65 72 7d 3a 22 0a  d ${leftover}:".
0450: 09 09 09 09 67 72 65 70 20 22 24 7b 6c 65 66 74  ....grep "${left
0460: 6f 76 65 72 7d 22 20 22 24 40 22 20 7c 20 73 65  over}" "$@" | se
0470: 64 20 27 73 40 5e 40 20 20 20 20 40 27 0a 09 09  d 's@^@    @'...
0480: 09 09 65 63 68 6f 20 27 27 0a 09 09 09 09 3b 3b  ..echo ''.....;;
0490: 0a 09 09 09 2a 29 0a 09 09 09 09 65 63 68 6f 20  ....*).....echo 
04a0: 22 55 6e 66 72 65 65 64 20 6d 65 6d 6f 72 79 20  "Unfreed memory 
04b0: 24 7b 6c 65 66 74 6f 76 65 72 7d 3a 22 0a 09 09  ${leftover}:"...
04c0: 09 09 67 72 65 70 20 22 24 7b 6c 65 66 74 6f 76  ..grep "${leftov
04d0: 65 72 7d 22 20 22 24 40 22 20 7c 20 73 65 64 20  er}" "$@" | sed 
04e0: 27 73 40 5e 40 20 20 20 20 40 27 0a 09 09 09 09  's@^@    @'.....
04f0: 65 63 68 6f 20 27 27 0a 09 09 09 09 3b 3b 0a 09  echo ''.....;;..
0500: 09 65 73 61 63 0a 09 64 6f 6e 65 0a 64 6f 6e 65  .esac..done.done
0510: 0a 0a 72 6d 20 2d 66 20 22 24 7b 54 4d 50 46 49  ..rm -f "${TMPFI
0520: 4c 45 7d 22 0a                                   LE}".