Skip to contentSkip to conversation

py: fix missing decode/encode of strings

PATCH Applied Eric Garver 3 participants 3 messages May 10, 2019 – May 12, 2019 netfilter

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 py: fix missing decode/encode of strings

When calling ffi functions, if the string is unicode we need to convert to utf-8. Then convert back for any output we receive.

Fixes 586ad210368b7 ("libnftables: Implement JSON parser")

---
v2: allow python2 support by checking if the string is already "bytes". python2 str uses bytes, python3 str uses unicode.

---
 py/nftables.py | 7 +++++++
 1 file changed, 7 insertions(+)
py/nftables.py +7 -0
index f07163573f9a..33cd2dfd736d 100644
@@ -352,9 +352,16 @@ class Nftables:
         output -- a string containing output written to stdout
         error  -- a string containing output written to stderr
         """
+        cmdline_is_unicode = False
+        if not isinstance(cmdline, bytes):
+            cmdline_is_unicode = True
+            cmdline = cmdline.encode("utf-8")
         rc = self.nft_run_cmd_from_buffer(self.__ctx, cmdline)
         output = self.nft_ctx_get_output_buffer(self.__ctx)
         error = self.nft_ctx_get_error_buffer(self.__ctx)
+        if cmdline_is_unicode:
+            output = output.decode("utf-8")
+            error = error.decode("utf-8")
 
         return (rc, output, error)
 
Signature · 1 line
2.20.1
maintainer Applied
#2 lore raw
In reply to Eric Garver
Quoted 2 lines · Eric Garver

When calling ffi functions, if the string is unicode we need to convert to utf-8. Then convert back for any output we receive.

Applied, thanks Eric.