]> git.ipfire.org Git - thirdparty/nftables.git/commitdiff
py: fix missing decode/encode of strings
authorEric Garver <eric@garver.life>
Fri, 10 May 2019 12:29:47 +0000 (08:29 -0400)
committerPablo Neira Ayuso <pablo@netfilter.org>
Sun, 12 May 2019 18:22:36 +0000 (20:22 +0200)
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")
Signed-off-by: Eric Garver <eric@garver.life>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
py/nftables.py

index f07163573f9a6a940f922632bfb5bfa0a1919496..33cd2dfd736d49da7667588e1917d84d38dbc7de 100644 (file)
@@ -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)