Skip to contentSkip to conversation

py: fix missing decode/encode of strings

PATCH Superseded Eric Garver 2 participants 3 messages May 1, 2019 – May 3, 2019 netfilter

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

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

When calling ffi functions we need to convert from python strings to utf-8. Then convert back for any output we receive.

Fixes 586ad210368b7 ("libnftables: Implement JSON parser")
---
 py/nftables.py | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
py/nftables.py +3 -3
index f07163573f9a..dea417e587d6 100644
@@ -352,9 +352,9 @@ class Nftables:
         output -- a string containing output written to stdout
         error  -- a string containing output written to stderr
         """
-        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)
+        rc = self.nft_run_cmd_from_buffer(self.__ctx, cmdline.encode("utf-8"))
+        output = self.nft_ctx_get_output_buffer(self.__ctx).decode("utf-8")
+        error = self.nft_ctx_get_error_buffer(self.__ctx).decode("utf-8")
 
         return (rc, output, error)
 
Signature · 1 line
2.20.1
reviewer
#1 lore raw

Hi,

Quoted 2 lines · Eric Garver

When calling ffi functions we need to convert from python strings to utf-8. Then convert back for any output we receive.

So the problem is passing utf-8 encoded strings as command?

[...]

py/nftables.py class Nftables: · line 357

Quoted 6 lines
py/nftables.py +3 -3
-        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)
+        rc = self.nft_run_cmd_from_buffer(self.__ctx, cmdline.encode("utf-8"))
+        output = self.nft_ctx_get_output_buffer(self.__ctx).decode("utf-8")
+        error = self.nft_ctx_get_error_buffer(self.__ctx).decode("utf-8")

Should the encoding be made configurable? I see encode() and decode() parameters are optional, but as soon as I call them with a string containing umlauts I get errors. So not sure if that would be an alternative.

BTW, thanks for all these fixes!

Cheers, Phil

#2 lore raw
Quoted 7 lines · Phil Sutter

Hi,

Quoted 2 lines · Eric Garver

When calling ffi functions we need to convert from python strings to utf-8. Then convert back for any output we receive.

So the problem is passing utf-8 encoded strings as command?

In python3 strings are unicode. But we need "bytes" when calling the ctypes function since it's imported with "c_char_p". This is what encode() is doing for us.

https://docs.python.org/3/library/ctypes.html#fundamental-data-types

In python2 strings are a sequence of bytes already. I'll have to v2 to if we care about python2 support.

Quoted 9 lines

[...]

py/nftables.py class Nftables: · line 357

Quoted 6 lines
py/nftables.py +3 -3
-        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)
+        rc = self.nft_run_cmd_from_buffer(self.__ctx, cmdline.encode("utf-8"))
+        output = self.nft_ctx_get_output_buffer(self.__ctx).decode("utf-8")
+        error = self.nft_ctx_get_error_buffer(self.__ctx).decode("utf-8")
Quoted 4 lines

Should the encoding be made configurable? I see encode() and decode() parameters are optional, but as soon as I call them with a string containing umlauts I get errors. So not sure if that would be an alternative.

I don't think so. Since we're calling system level stuff (nftables, kernel) I think utf-8 is what we want.

Encoding with utf-8 does the right thing:

python3:

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

"ö".encode("utf-8")
b'\xc3\xb6'

python2:

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

u"ö".encode("utf-8")
'\xc3\xb6'