diff -ur rtmpdump~/ChangeLog rtmpdump/ChangeLog
--- rtmpdump~/ChangeLog	2012-03-31 17:03:17.000000000 -0500
+++ rtmpdump/ChangeLog	2012-03-31 19:31:12.090798665 -0500
@@ -4,6 +4,11 @@
 Copyright 2009 The Flvstreamer Team
 http://rtmpdump.mplayerhq.hu/
 
+31 March 2012
+- fix buffer overrun on more than 255 duplicates
+- use unique filenames instead of overwriting existing files on disk
+- patch by Andy Goth <andrew.m.goth@gmail.com>
+
 20 July 2011
 - add NetStream.Authenticate.UsherToken for Justin.tv
 
diff -ur rtmpdump~/rtmpsuck.c rtmpdump/rtmpsuck.c
--- rtmpdump~/rtmpsuck.c	2012-03-31 17:03:17.000000000 -0500
+++ rtmpdump/rtmpsuck.c	2012-03-31 19:57:45.181073341 -0500
@@ -28,6 +28,8 @@
 #include <string.h>
 #include <math.h>
 #include <limits.h>
+#include <sys/types.h>
+#include <dirent.h>
 
 #include <signal.h>
 #include <getopt.h>
@@ -316,6 +318,7 @@
          0x00, 0x00, 0x00, 0x00      // first prevTagSize=0
        };
       int count = 0, flen;
+      DIR *dirp;
 
       server->rc.m_stream_id = pack->m_nInfoField2;
       AMFProp_GetString(AMF_GetProp(&obj, NULL, 3), &av);
@@ -359,9 +362,17 @@
           av.av_len--;
         }
       flen = av.av_len;
-      /* hope there aren't more than 255 dups */
-      if (count)
-        flen += 2;
+      /* hope there aren't more than 4294967295 dups :^) */
+      /* increase filename buffer size if there are duplicates */
+      if (count && count<255)
+	flen += 2;
+      else if (count > 255)
+	{
+	  /* count hexadecimal digits, known to be at least three */
+	  int bits;
+	  for (bits=12; bits<32 && count>>bits; bits+=4);
+	  flen += bits >> 2;
+	}
       file = malloc(flen+1);
 
       memcpy(file, av.av_val, av.av_len);
@@ -372,6 +383,39 @@
       for (p=file; *p; p++)
         if (*p == ':')
           *p = '_';
+      /* check for duplicate filenames on disk */
+      if ((dirp = opendir(".")))
+	{
+	  struct dirent *entp;
+	  int bits = 0;
+	  count = 0;
+	  while ((entp = readdir(dirp)))
+	    {
+	      /* whenever a duplicate is found, change filename and restart
+	       * duplicate search until there are no duplicates */
+	      if (!strcmp(file, entp->d_name))
+		{
+		  /* grow filename buffer by two bytes if this is the first
+		   * duplicate or one byte if we've hit a power of sixteen */
+		  if (!((count+1)&((1<<bits)-1)))
+		    {
+		      if (!count)
+		      {
+			file[flen] = '-';
+			flen++;
+		      }
+		      flen++;
+		      file = realloc(file, flen+1);
+		      file[flen] = '\0';
+		      bits += 4;
+		    }
+		  sprintf(file+flen-(bits>>2), "%x", count);
+		  count++;
+		  rewinddir(dirp);
+		}
+	    }
+	  closedir(dirp);
+	}
       RTMP_LogPrintf("Playpath: %.*s\nSaving as: %s\n",
         server->rc.Link.playpath.av_len, server->rc.Link.playpath.av_val,
         file);
