From 6d7fa91949337c7a86bab3359b39558fdae07dce Mon Sep 17 00:00:00 2001
From: Michael Schroeder <mls@suse.de>
Date: Fri, 23 Oct 2020 14:02:35 +0200
Subject: [PATCH] Fix logic error in grabArgs()

If there was a \ at the end of the buffer, the code would
return a pointer after the trailing \0 leading to unallocated
memory access and weird results in some cases.

See commit 817959609b95afe34ce0f7f6c3dc5d7d0d9a8470.
---
 rpmio/macro.c     | 2 +-
 tests/rpmmacro.at | 5 +++++
 2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/rpmio/macro.c b/rpmio/macro.c
index 1edcb39e6..a1ed9b288 100644
--- a/rpmio/macro.c
+++ b/rpmio/macro.c
@@ -947,7 +947,7 @@ grabArgs(MacroBuf mb, const rpmMacroEntry me, const char * se,
 	splitQuoted(&argv, s, " \t");
 	free(s);
 
-	cont = ((*lastc == '\0' || *lastc == '\n') && *(lastc-1) != '\\') ?
+	cont = (*lastc == '\0') || (*lastc == '\n' && *(lastc-1) != '\\') ?
 	       lastc : lastc + 1;
     }
 
diff --git a/tests/rpmmacro.at b/tests/rpmmacro.at
index d972a1197..a21952c46 100644
--- a/tests/rpmmacro.at
+++ b/tests/rpmmacro.at
@@ -179,6 +179,9 @@ runroot rpm \
     --eval '%foo %{quote:   2 3  5} %{quote:%{nil}}' \
     --eval '%foo x%{quote:y}z 123' \
     --eval '%foo x%{quote:%{nil}}z' \
+    --eval '%foo 1 \
+bar' \
+    --eval '%foo 1 \' \
 ],
 [0],
 [1:"1"
@@ -190,6 +193,8 @@ runroot rpm \
 2:"   2 3  5" ""
 2:"xyz" "123"
 1:"xz"
+2:"1" "\"bar
+2:"1" "\"
 ])
 AT_CLEANUP
 
-- 
2.27.0

