diff --git a/hpvm/test/benchmarks/hpvm-cava/src/main.c b/hpvm/test/benchmarks/hpvm-cava/src/main.c
index 0d8fb37d0103b55234e59e8d2b66742faed207b6..35fc638080ee7482b173fe4bec515717648de822 100644
--- a/hpvm/test/benchmarks/hpvm-cava/src/main.c
+++ b/hpvm/test/benchmarks/hpvm-cava/src/main.c
@@ -12,6 +12,9 @@
 
 #include "hpvm.h"
 
+// Max file extension size
+#define MAX_EXT_SIZE 20
+
 int NUM_TEST_CASES;
 int NUM_CLASSES;
 int INPUT_DIM;
@@ -875,29 +878,33 @@ int main(int argc, char *argv[]) {
 
   // Output the image.
   // NOTE: We deliberately perform this file I/O outside of the kernel.
-  char str[50], base_str[50];
+  const int len = strlen(args.args[OUTPUT_IMAGE_BIN]);
+  const char *base_str = args.args[OUTPUT_IMAGE_BIN];
+  char *str = malloc(sizeof(char)*len + MAX_EXT_SIZE + 1); // Handles the extensions below
   strcpy(base_str, args.args[OUTPUT_IMAGE_BIN]);
   strcpy(str, base_str);
-  strcat(str, ".bin");
+  strncat(str, ".bin", MAX_EXT_SIZE);
   printf("Writing output image to %s\n", str);
   write_image_to_binary(str, image_out, row_size, col_size);
   strcpy(str, base_str);
-  strcat(str, "_gamut.bin");
+  strncat(str, "_gamut.bin", MAX_EXT_SIZE);
   printf("Writing output image to %s\n", str);
   write_image_to_binary(str, image_out_gamut, row_size, col_size);
   strcpy(str, base_str);
-  strcat(str, "_demosaic.bin");
+  strncat(str, "_demosaic.bin", MAX_EXT_SIZE);
   printf("Writing output image to %s\n", str);
   write_image_to_binary(str, image_out_demosaic, row_size, col_size);
   strcpy(str, base_str);
-  strcat(str, "_denoise.bin");
+  strncat(str, "_denoise.bin", MAX_EXT_SIZE);
   printf("Writing output image to %s\n", str);
   write_image_to_binary(str, image_out_denoise, row_size, col_size);
   strcpy(str, base_str);
-  strcat(str, "_transform.bin");
+  strncat(str, "_transform.bin", MAX_EXT_SIZE);
   printf("Writing output image to %s\n", str);
   write_image_to_binary(str, image_out_transform, row_size, col_size);
 
+  free(str);
+
   __hpvm__cleanup();
 
   return 0;