From e5a537efabc9d66c6afa3d54eb35156093baa0aa Mon Sep 17 00:00:00 2001 From: Michel Hidalgo Date: Mon, 11 Feb 2019 17:39:53 -0300 Subject: [PATCH 1/2] Enforce UTF8 argv on rclpy.init() Signed-off-by: Michel Hidalgo --- rclpy/src/rclpy/_rclpy.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/rclpy/src/rclpy/_rclpy.c b/rclpy/src/rclpy/_rclpy.c index b92426755..4a28eec73 100644 --- a/rclpy/src/rclpy/_rclpy.c +++ b/rclpy/src/rclpy/_rclpy.c @@ -501,6 +501,12 @@ rclpy_init(PyObject * Py_UNUSED(self), PyObject * args) } // Borrows a pointer, do not free arg_values[i] arg_values[i] = PyUnicode_AsUTF8(pyarg); + if (NULL == arg_values[i]) { + PyErr_Format( + PyExc_RuntimeError, "Bad arguments string encoding (not UTF8)"); + have_args = false; + break; + } } } From c030ef4cde0d1e3b949a4dc8a976eaa2af6b7312 Mon Sep 17 00:00:00 2001 From: Michel Hidalgo Date: Tue, 12 Feb 2019 11:27:59 -0300 Subject: [PATCH 2/2] Addresses peer review comments. - Remove redundant CPython error setting. - Add a test case for non decodable UTF-8 in cmd args. Signed-off-by: Michel Hidalgo --- rclpy/src/rclpy/_rclpy.c | 2 -- rclpy/test/test_init_shutdown.py | 20 +++++++++++++++++++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/rclpy/src/rclpy/_rclpy.c b/rclpy/src/rclpy/_rclpy.c index 4a28eec73..ee79612e3 100644 --- a/rclpy/src/rclpy/_rclpy.c +++ b/rclpy/src/rclpy/_rclpy.c @@ -502,8 +502,6 @@ rclpy_init(PyObject * Py_UNUSED(self), PyObject * args) // Borrows a pointer, do not free arg_values[i] arg_values[i] = PyUnicode_AsUTF8(pyarg); if (NULL == arg_values[i]) { - PyErr_Format( - PyExc_RuntimeError, "Bad arguments string encoding (not UTF8)"); have_args = false; break; } diff --git a/rclpy/test/test_init_shutdown.py b/rclpy/test/test_init_shutdown.py index ee14c6797..e8360ccec 100644 --- a/rclpy/test/test_init_shutdown.py +++ b/rclpy/test/test_init_shutdown.py @@ -33,11 +33,25 @@ def func_init(): rclpy.init(context=context) except RuntimeError: return False - rclpy.shutdown(context=context) return True +def func_init_with_non_utf8_arguments(): + import rclpy + context = rclpy.context.Context() + # Embed non decodable characters e.g. due to + # wrong locale settings. + # See PEP-383 for further reference. + args = ['my-node.py', 'Ragnar\udcc3\udcb6k'] + try: + rclpy.init(context=context, args=args) + except UnicodeEncodeError: + return True + rclpy.shutdown(context=context) + return False + + def func_init_shutdown(): import rclpy context = rclpy.context.Context() @@ -124,6 +138,10 @@ def test_init(): func_launch(func_init, 'failed to initialize rclpy') +def test_init_with_non_utf8_arguments(): + func_launch(func_init_with_non_utf8_arguments, 'handled non UTF-8 arguments') + + def test_init_shutdown(): func_launch(func_init_shutdown, 'failed to shutdown rclpy')