From b36967a836f7468165c29a851a510bce08d410d2 Mon Sep 17 00:00:00 2001
From: Joel <rootjdev@gmail.com>
Date: Wed, 17 Aug 2022 09:18:10 +0930
Subject: [PATCH] dev 1.3.1-rc.3

---
 Changelog              |  5 +++++
 Makefile.am            |  4 +++-
 configure.ac           |  2 +-
 test/tap/call.tap      | 23 +++++++++++++++++++++++
 test/tools/asmlineP.sh | 16 ++++++++++++++++
 tools/asmline.c        | 16 +++++++---------
 6 files changed, 55 insertions(+), 11 deletions(-)
 create mode 100644 test/tap/call.tap
 create mode 100755 test/tools/asmlineP.sh

diff --git a/Changelog b/Changelog
index 0e23887..b8b0b91 100644
--- a/Changelog
+++ b/Changelog
@@ -1,3 +1,8 @@
+version 1.3.1-rc.3 (2022-08-17)
+
+	- fixed a bug which caused writing to a file via the -P switch to cause a
+	  segmentation fault
+	 
 version 1.3.1-rc.2 (2022-08-09)
 
 	- added four encodings for {vmovdqu, vmovupd} xmmN {xmmN,m/128}
diff --git a/Makefile.am b/Makefile.am
index 65a7920..ae394f3 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -127,6 +127,7 @@ TAP_LOG_COMPILER = ./test/tap/compiler.sh
 
 # add TAP tests here
 TEST_TAP = \
+		test/tap/call.tap \
 		test/tap/cmp.tap \
 		test/tap/imul.tap \
 		test/tap/lea.tap \
@@ -154,7 +155,8 @@ XFAIL_TESTS= $(TEST_EAF) \
 		test/tap/nasm_incompatible.tap
 
 # add SH-tests here
-TEST_SH = test/tools/asmline.sh
+TEST_SH = test/tools/asmline.sh \
+		  test/tools/asmlineP.sh
 
 
 # add .c -tests here
diff --git a/configure.ac b/configure.ac
index 7d76524..5e224e0 100644
--- a/configure.ac
+++ b/configure.ac
@@ -2,7 +2,7 @@
 # Process this file with autoconf to produce a configure script.
 
 AC_PREREQ([2.69])
-AC_INIT([assemblyline],[1.3.1-rc.2],[yval@cs.adelaide.edu.au])
+AC_INIT([assemblyline],[1.3.1-rc.3],[yval@cs.adelaide.edu.au])
 AC_CONFIG_HEADERS([config.h])
 AC_CONFIG_SRCDIR([src/assemblyline.c])
 AC_CONFIG_AUX_DIR([build-aux])
diff --git a/test/tap/call.tap b/test/tap/call.tap
new file mode 100644
index 0000000..da29dea
--- /dev/null
+++ b/test/tap/call.tap
@@ -0,0 +1,23 @@
+push rax
+push rcx
+push rdx
+push r8
+push r9
+push r10
+mov rcx, 0x637ea511
+mov rdx, 0x637ea4d1
+mov r8, 0x637ea4f1
+mov r9, 0x0
+push 0x0
+push 0x0
+mov r10, 0x61813ff0
+call r10
+pop rcx
+pop rcx
+pop r10
+pop r9
+pop r8
+pop rdx
+pop rcx
+pop rax
+ret
diff --git a/test/tools/asmlineP.sh b/test/tools/asmlineP.sh
new file mode 100755
index 0000000..d60e6e9
--- /dev/null
+++ b/test/tools/asmlineP.sh
@@ -0,0 +1,16 @@
+#!/usr/bin/env bash
+
+# error out on any error
+set -e
+
+tool=./tools/asmline
+# should not error out without option to '-r'
+${tool} -r <<EOF
+ret
+EOF
+
+# should not error out without option to '-P /dev/stdout'
+
+${tool} -P /dev/stdout <<EOF
+ret
+EOF
diff --git a/tools/asmline.c b/tools/asmline.c
index 48bb81a..545f401 100644
--- a/tools/asmline.c
+++ b/tools/asmline.c
@@ -314,11 +314,10 @@ static int create_binary_file(assemblyline_t al, enum OUTPUT create_bin,
   switch (create_bin) {
 
   case BIN_FILE: {
-    char bin_ext[] = ".bin";
-    size_t bin_file_len = strlen(param_file) + strlen(bin_ext) + 1;
-    char *bin_file = calloc(bin_file_len, sizeof(char));
-    sprintf(bin_file, "%s%s", param_file, bin_ext);
-    write_file = bin_file;
+    const size_t len_ext = 5; // 4chars for '.bin', 1 for \0
+    size_t bin_file_len = strlen(param_file) + len_ext;
+    write_file = calloc(bin_file_len, sizeof(char));
+    snprintf(write_file, bin_file_len, "%s.bin", param_file);
   } break;
 
   case GENERIC_FILE:
@@ -334,14 +333,12 @@ static int create_binary_file(assemblyline_t al, enum OUTPUT create_bin,
     fprintf(stderr, "failed to create %s\n", param_file);
     ret = EXIT_FAILURE;
   }
-
-  if (write_file != NULL)
+  // free if we've allocated that filename
+  if (create_bin == BIN_FILE)
     free(write_file);
   return ret;
 }
 
-/** enum mode { M_STDIN, M_STDIN_COUNT, M_FILE, M_FILE_COUNT }; */
-
 struct mode {
   enum src { STD, FLE } src : 1;
   bool count : 1;
@@ -364,6 +361,7 @@ struct mode findMode(struct parsed_ops *ops, int argc) {
   }
   return ret;
 }
+
 int main(int argc, char *argv[]) {
 
   int total_chunk_brks = -1;