diff --git a/src/avr.c b/src/avr.c
index 9d2bf503..a6d621b7 100644
--- a/src/avr.c
+++ b/src/avr.c
@@ -278,6 +278,7 @@ int avr_read_byte_default(PROGRAMMER * pgm, AVRPART * p, AVRMEM * mem,
  * value. This is useful for determining where to stop when dealing
  * with "flash" memory, since writing 0xff to flash is typically a
  * no-op. Always return an even number since flash is word addressed.
+ * Only apply this optimisation on flash-type memory.
  */
 int avr_mem_hiaddr(AVRMEM * mem)
 {
@@ -293,6 +294,13 @@ int avr_mem_hiaddr(AVRMEM * mem)
   if(disableffopt)
     return mem->size;
 
+  /* if the memory is not a flash-type memory do not remove trailing 0xff */
+  if(strcasecmp(mem->desc, "flash") &&
+     strcasecmp(mem->desc, "application") &&
+     strcasecmp(mem->desc, "apptable") &&
+     strcasecmp(mem->desc, "boot"))
+    return mem->size;
+
   /* return the highest non-0xff address regardless of how much
      memory was read */
   for (i=mem->size-1; i>0; i--) {
@@ -426,15 +434,8 @@ int avr_read(PROGRAMMER * pgm, AVRPART * p, char * memtype,
       nread++;
       report_progress(nread, npages, NULL);
     }
-    if (!failure) {
-      if (strcasecmp(mem->desc, "flash") == 0 ||
-          strcasecmp(mem->desc, "application") == 0 ||
-          strcasecmp(mem->desc, "apptable") == 0 ||
-          strcasecmp(mem->desc, "boot") == 0)
-        return avr_mem_hiaddr(mem);
-      else
-        return mem->size;
-    }
+    if (!failure)
+      return avr_mem_hiaddr(mem);
     /* else: fall back to byte-at-a-time write, for historical reasons */
   }
 
@@ -464,13 +465,7 @@ int avr_read(PROGRAMMER * pgm, AVRPART * p, char * memtype,
     report_progress(i, mem->size, NULL);
   }
 
-  if (strcasecmp(mem->desc, "flash") == 0 ||
-      strcasecmp(mem->desc, "application") == 0 ||
-      strcasecmp(mem->desc, "apptable") == 0 ||
-      strcasecmp(mem->desc, "boot") == 0)
-    return avr_mem_hiaddr(mem);
-  else
-    return i;
+  return avr_mem_hiaddr(mem);
 }
 
 
diff --git a/src/fileio.c b/src/fileio.c
index 429a200e..754e267b 100644
--- a/src/fileio.c
+++ b/src/fileio.c
@@ -1585,21 +1585,14 @@ int fileio(int op, char * filename, FILEFMT format,
       return -1;
   }
 
-  if (rc > 0) {
-    if ((op == FIO_READ) && (strcasecmp(mem->desc, "flash") == 0 ||
-                             strcasecmp(mem->desc, "application") == 0 ||
-                             strcasecmp(mem->desc, "apptable") == 0 ||
-                             strcasecmp(mem->desc, "boot") == 0)) {
-      /*
-       * if we are reading flash, just mark the size as being the
-       * highest non-0xff byte
-       */
-      int hiaddr = avr_mem_hiaddr(mem);
+  /* on reading flash set the size to location of highest non-0xff byte */
+  if (rc > 0 && op == FIO_READ) {
+    int hiaddr = avr_mem_hiaddr(mem);
 
-      if(hiaddr < rc)           /* if trailing-0xff not disabled */
-        rc = hiaddr;
-    }
+    if(hiaddr < rc)             /* if trailing-0xff not disabled */
+      rc = hiaddr;
   }
+
   if (format != FMT_IMM && !using_stdio) {
     fclose(f);
   }