Skip to contentSkip to conversation

Handle patch trailers with special characters

PATCH Applied Brandon Maier 2 participants 8 messages Oct 27, 2024 – Jan 22, 2025

lore
Share & tools
lore
#0 lore raw

v2 cover Handle patch trailers with special characters

Fixes bug reported in https://github.com/mricon/b4/issues/50

Patch trailers that contain commas are not parsed correctly, this causes the trailer to be set to a blank string. The issue is special characters in emails are supposed to be quoted per RFC 2822. But it's fairly common for patch trailers to be hand-written and be improperly formatted.

The trailer tests were hiding some of the parsing issues, so the first few patches fix up the test.

While fixing this bug I discovered the trailer parser also breaks even when the trailer is properly quoted, so I fix that in this series as well.

---
Changes in v2:
- Fixed other tests that broke when the email parsing changed
- Link to v1: https://patch.msgid.link/[email protected]

---
Brandon Maier (6):
src/tests: check trailer extinfo
src/tests: fix trailer test hiding parser errors
b4: handle trailers with improper formatting src/tests: test for trailers with improper formatting
b4: handle trailers with special characters src/tests: test for trailers with special characters

 src/b4/__init__.py                                          |  6 +++---
 .../samples/trailers-followup-bare-address-ref-defaults.txt |  2 +-
 .../samples/trailers-followup-name-parens-ref-defaults.txt  |  2 +-
 src/tests/samples/trailers-test-extinfo.txt                 |  1 +
 src/tests/samples/trailers-test-simple.txt                  |  1 +
 src/tests/test___init__.py                                  | 13 ++++++++++---
 6 files changed, 17 insertions(+), 8 deletions(-)

---
base-commit: 2a6338e451a0c1e81f214f48c820c1e52d76b2f1
change-id: 20241027-trailer-special-chars-78b97ff63ea9

Best regards,

Signature · 1 line
Brandon Maier <[email protected]>
#1 lore raw

PATCH v2 1/6 src/tests: check trailer extinfo

The b4.LoreTrailer.__eq__ ignores the 'extinfo' of a trailer. So the trailer test is not checking that extinfo is parsed correctly.

---
 src/tests/test___init__.py | 1 +
 1 file changed, 1 insertion(+)
src/tests/test___init__.py +1 -0
index 297e5120a8c8adf241660f06e9a83c1ff4a0fd3a..dbc92c7e8352728c2f2a951f7b9b1496ccbaf2c5 100644
@@ -79,6 +79,7 @@ def test_parse_trailers(sampledir, source, expected):
             mytype, myname, myvalue, myextinfo = expected.pop(0)
             mytr = b4.LoreTrailer(name=myname, value=myvalue, extinfo=myextinfo)
             assert tr == mytr
+            assert tr.extinfo == mytr.extinfo
             assert tr.type == mytype
 
 
Signature · 1 line
2.47.0
#2 lore raw

PATCH v2 2/6 src/tests: fix trailer test hiding parser errors

In reply to Brandon Maier

The trailer parsing test compares the values of the b4.LoreTrailer object created from the test input against a b4.LoreTrailer object created from our expected test input.

This means if there is an error in how b4.LoreTrailer parses input, it won't be detected. This was discovered from a reported bug with parsing email trailers with embedded ','.

---
 src/tests/test___init__.py | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
src/tests/test___init__.py +6 -2
index dbc92c7e8352728c2f2a951f7b9b1496ccbaf2c5..7fe43b0158460f06d0a2b52d2689d26e7ef7f840 100644
@@ -57,7 +57,7 @@ def test_save_git_am_mbox(sampledir, tmp_path, source, regex, flags, ismbox):
 
 @pytest.mark.parametrize('source,expected', [
     ('trailers-test-simple',
-     [('person', 'Reviewed-By', 'Bogus Bupkes <[email protected]>', None),
+     [('person', 'Reviewed-by', 'Bogus Bupkes <[email protected]>', None),
       ('utility', 'Fixes', 'abcdef01234567890', None),
       ('utility', 'Link', 'https://msgid.link/[email protected]', None),
       ]),
@@ -77,10 +77,14 @@ def test_parse_trailers(sampledir, source, expected):
         assert len(expected) == len(trs)
         for tr in trs:
             mytype, myname, myvalue, myextinfo = expected.pop(0)
+            assert tr.name == myname
+            assert tr.value == myvalue
+            assert tr.extinfo == myextinfo
+            assert tr.type == mytype
+
             mytr = b4.LoreTrailer(name=myname, value=myvalue, extinfo=myextinfo)
             assert tr == mytr
             assert tr.extinfo == mytr.extinfo
-            assert tr.type == mytype
 
 
 @pytest.mark.parametrize('source,serargs,amargs,reference,b4cfg', [
Signature · 1 line
2.47.0
#3 lore raw

PATCH v2 3/6 b4: handle trailers with improper formatting

In reply to Brandon Maier

A patch trailer with an improperly formatted email parses to an empty string. For example see the linked github issue, where a trailer's display name contains an unquoted comma.

As patch trailers are often hand written this can happen frequently. Try to gracefully handle this case by checking if an email parses correctly, and if it doesn't fallback to displaying it verbatim.

---
 src/b4/__init__.py | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
src/b4/__init__.py +3 -1
index 5115a9cd9b8746f84eb104a2be69fe121724167e..cf112e3bce2911c78cff18aac267608f796957ab 100644
@@ -1063,8 +1063,10 @@ def __init__(self, name: Optional[str] = None, value: Optional[str] = None, exti
                 # Normalize the value with parsed data
                 if self.addr[0]:
                     self.value = f'{self.addr[0]} <{self.addr[1]}>'
-                else:
+                elif self.addr[1]:
                     self.value = self.addr[1]
+                else:
+                    self.type = 'unknown'
             else:
                 self.type = 'unknown'
         self.lname = self.name.lower()
Signature · 1 line
2.47.0
#4 lore raw

PATCH v2 4/6 src/tests: test for trailers with improper formatting

In reply to Brandon Maier

Check that a trailer with an improperly formatted email address will still parse to something usable.

---
 src/tests/samples/trailers-test-extinfo.txt | 1 +
 src/tests/test___init__.py                  | 3 ++-
 2 files changed, 3 insertions(+), 1 deletion(-)
src/tests/samples/trailers-test-extinfo.txt +1 -0
index aa61e8d635923c2e2539b82c60f69b489ad7cee3..5417cdd87bfdf4ddb278e125a6dd04d2e86af141 100644
@@ -4,6 +4,7 @@ Date: Tue, 30 Aug 2022 11:19:07 -0400
 
 This is a simple trailer parsing test.
 
+Reported-by: Some, One <[email protected]>
 Reviewed-by: Bogus Bupkes <[email protected]>
 [for the parts that are bogus]
 Fixes: abcdef01234567890
src/tests/test___init__.py +2 -1
index 7fe43b0158460f06d0a2b52d2689d26e7ef7f840..85ccd190677daebc30b9987eb811d44a15dce79b 100644
@@ -62,7 +62,8 @@ def test_save_git_am_mbox(sampledir, tmp_path, source, regex, flags, ismbox):
       ('utility', 'Link', 'https://msgid.link/[email protected]', None),
       ]),
     ('trailers-test-extinfo',
-     [('person', 'Reviewed-by', 'Bogus Bupkes <[email protected]>', '[for the parts that are bogus]'),
+     [('unknown', 'Reported-by', 'Some, One <[email protected]>', None),
+      ('person', 'Reviewed-by', 'Bogus Bupkes <[email protected]>', '[for the parts that are bogus]'),
       ('utility', 'Fixes', 'abcdef01234567890', None),
       ('person', 'Tested-by', 'Some Person <[email protected]>', '           [this person visually indented theirs]'),
       ('utility', 'Link', 'https://msgid.link/[email protected]', '  # initial submission'),
Signature · 1 line
2.47.0
#5 lore raw

PATCH v2 5/6 b4: handle trailers with special characters

In reply to Brandon Maier

Parsing an email trailer with special characters breaks the formatting. For example this trailer.

Quoted 1 line

Will get parsed into this.

Quoted 1 line

This is because b4.LoreTrailer attempts to reformat the trailer using a naive string format. Instead use the Python standard library formataddr function which correctly handles special characters.

---
 src/b4/__init__.py                                                | 6 ++----
 src/tests/samples/trailers-followup-bare-address-ref-defaults.txt | 2 +-
 src/tests/samples/trailers-followup-name-parens-ref-defaults.txt  | 2 +-
 3 files changed, 4 insertions(+), 6 deletions(-)
src/b4/__init__.py +2 -4
index cf112e3bce2911c78cff18aac267608f796957ab..0785be8ffcef08fb5cd4c484a40e6fe856446cfe 100644
@@ -1061,10 +1061,8 @@ def __init__(self, name: Optional[str] = None, value: Optional[str] = None, exti
                 self.type = 'person'
                 self.addr = email.utils.parseaddr(value)
                 # Normalize the value with parsed data
-                if self.addr[0]:
-                    self.value = f'{self.addr[0]} <{self.addr[1]}>'
-                elif self.addr[1]:
-                    self.value = self.addr[1]
+                if self.addr[0] or self.addr[1]:
+                    self.value = email.utils.formataddr(self.addr)
                 else:
                     self.type = 'unknown'
             else:
src/tests/samples/trailers-followup-bare-address-ref-defaults.txt +1 -1
index 7b1ebef3c9d9b9c60288dc07bcf7f82f066c6934..82e1576019bd9f4d69f9a2ed9b783340a5ea16b2 100644
@@ -14,7 +14,7 @@ Cc: [email protected]
 Reviewed-by: Original Reviewer <[email protected]>
 Link: https://msgid.link/[email protected]
 Signed-off-by: Original Submitter <[email protected]>
-Reviewed-by: Followup Reviewer1 (corporate) <[email protected]>
+Reviewed-by: "Followup Reviewer1 (corporate)" <[email protected]>
 Tested-by: Followup Reviewer2 <[email protected]>
 
 diff --git a/b4/junk.py b/b4/junk.py
src/tests/samples/trailers-followup-name-parens-ref-defaults.txt +1 -1
index 06b883126bd3ea9f7a4d22c58fc10e9e89d6bde7..dfc013c89c415107ff2517c1715e523982df2afc 100644
@@ -13,7 +13,7 @@ Fixes: abcdef01234567890
 Reviewed-by: Original Reviewer <[email protected]>
 Link: https://msgid.link/[email protected]
 Signed-off-by: Original Submitter <[email protected]>
-Reviewed-by: Followup Reviewer1 (corporate) <[email protected]>
+Reviewed-by: "Followup Reviewer1 (corporate)" <[email protected]>
 Tested-by: Followup Reviewer2 <[email protected]>
 
 diff --git a/b4/junk.py b/b4/junk.py
Signature · 1 line
2.47.0
#6 lore raw

PATCH v2 6/6 src/tests: test for trailers with special characters

In reply to Brandon Maier

Test that email trailers with special characters are parsed correctly.

---
 src/tests/samples/trailers-test-simple.txt | 1 +
 src/tests/test___init__.py                 | 3 ++-
 2 files changed, 3 insertions(+), 1 deletion(-)
src/tests/samples/trailers-test-simple.txt +1 -0
index da64caff8dfaa0cbc630c8ae3515f4508cfd0764..a82416354012d3c32f99b39d1918ac343f2b66cc 100644
@@ -4,6 +4,7 @@ Date: Tue, 30 Aug 2022 11:19:07 -0400
 
 This is a simple trailer parsing test.
 
+Reported-by: "Doe, Jane" <[email protected]>
 Reviewed-by: Bogus Bupkes <[email protected]>
 Fixes: abcdef01234567890
 Link: https://msgid.link/[email protected]
src/tests/test___init__.py +2 -1
index 85ccd190677daebc30b9987eb811d44a15dce79b..a4637b9c797367da462f062511fdf4e018a5b81e 100644
@@ -57,7 +57,8 @@ def test_save_git_am_mbox(sampledir, tmp_path, source, regex, flags, ismbox):
 
 @pytest.mark.parametrize('source,expected', [
     ('trailers-test-simple',
-     [('person', 'Reviewed-by', 'Bogus Bupkes <[email protected]>', None),
+     [('person', 'Reported-by', '"Doe, Jane" <[email protected]>', None),
+      ('person', 'Reviewed-by', 'Bogus Bupkes <[email protected]>', None),
       ('utility', 'Fixes', 'abcdef01234567890', None),
       ('utility', 'Link', 'https://msgid.link/[email protected]', None),
       ]),
Signature · 1 line
2.47.0
#7 lore raw
In reply to Brandon Maier
Quoted 8 lines · Brandon Maier

Fixes bug reported in https://github.com/mricon/b4/issues/50

Patch trailers that contain commas are not parsed correctly, this causes the trailer to be set to a blank string. The issue is special characters in emails are supposed to be quoted per RFC 2822. But it's fairly common for patch trailers to be hand-written and be improperly formatted.

[...]

Applied, thanks!

[1/6] src/tests: check trailer extinfo

      commit: 0d0d2e2e53013302d0625eefb4ee519166802c1b
[2/6] src/tests: fix trailer test hiding parser errors
      commit: 285445c83c066f8ba39657d8bf360df729b60002
[3/6] b4: handle trailers with improper formatting
      commit: cc2d6bfbc6d2b819d8d291c3b7470642893d69fa
[4/6] src/tests: test for trailers with improper formatting
      commit: e1ba36d1a62be7972c46668c89214dda00b8d6f9
[5/6] b4: handle trailers with special characters
      commit: 83327d2fbad9506afc2a3e30566025345e157eca
[6/6] src/tests: test for trailers with special characters
      commit: f147f985fa6ad6558603b83fa0a7bf38aeefd436

Best regards,

Signature · 1 line
Konstantin Ryabitsev <[email protected]>