From: Stefan Metzmacher Date: Fri, 12 Jun 2026 14:16:46 +0000 (+0200) Subject: pidl:Samba4/Python: use a C array size variable to cache PyList_GET_SIZE($py_var) X-Git-Url: http://git.ipfire.org/gitweb/?a=commitdiff_plain;h=a83fc8f9cf5ccbc8e88f4cc81ae35947790122bc;p=thirdparty%2Fsamba.git pidl:Samba4/Python: use a C array size variable to cache PyList_GET_SIZE($py_var) This allows the C compiler to notice that we correctly checked the size of fixed sized arrays. BUG: https://bugzilla.samba.org/show_bug.cgi?id=16006 Signed-off-by: Stefan Metzmacher Reviewed-by: Volker Lendecke --- diff --git a/pidl/lib/Parse/Pidl/Samba4/Python.pm b/pidl/lib/Parse/Pidl/Samba4/Python.pm index b342704af4d..383181dc104 100644 --- a/pidl/lib/Parse/Pidl/Samba4/Python.pm +++ b/pidl/lib/Parse/Pidl/Samba4/Python.pm @@ -2183,24 +2183,26 @@ sub ConvertObjectFromPythonLevel($$$$$$$$$) if (is_charset_array($e, $l)) { $self->ConvertStringFromPythonData($mem_ctx, $py_var, $var_name, $fail); } else { + my $size = "$e->{NAME}_size_$l->{LEVEL_INDEX}"; my $counter = "$e->{NAME}_cntr_$l->{LEVEL_INDEX}"; $self->pidl("PY_CHECK_TYPE(&PyList_Type, $py_var, $fail);"); $self->pidl("{"); $self->indent; - $self->pidl("int $counter;"); + $self->pidl("ssize_t $size = PyList_GET_SIZE($py_var);"); + $self->pidl("ssize_t $counter;"); if (ArrayDynamicallyAllocated($e, $l)) { - $self->pidl("$var_name = talloc_array_ptrtype($mem_ctx, $var_name, PyList_GET_SIZE($py_var));"); + $self->pidl("$var_name = talloc_array_ptrtype($mem_ctx, $var_name, $size);"); $self->pidl("if (!$var_name) { $fail }"); $self->pidl("talloc_set_name_const($var_name, \"ARRAY: $var_name\");"); } else { - $self->pidl("if (ARRAY_SIZE($var_name) != PyList_GET_SIZE($py_var)) {"); + $self->pidl("if (ARRAY_SIZE($var_name) != $size) {"); $self->indent; $self->pidl("PyErr_Format(PyExc_TypeError, \"Expected list of type %s, length %zu, got %zd\", Py_TYPE($py_var)->tp_name, ARRAY_SIZE($var_name), PyList_GET_SIZE($py_var));"); $self->pidl("$fail"); $self->deindent; $self->pidl("}"); } - $self->pidl("for ($counter = 0; $counter < PyList_GET_SIZE($py_var); $counter++) {"); + $self->pidl("for ($counter = 0; $counter < $size; $counter++) {"); $self->indent; if (ArrayDynamicallyAllocated($e, $l)) { $self->ConvertObjectFromPythonLevel($env, $var_name, "PyList_GET_ITEM($py_var, $counter)", $e, $nl, "($var_name)"."[$counter]", $fail, 0);