From: Chuck Lever Date: Fri, 16 May 2025 13:37:12 +0000 (-0400) Subject: xdrgen: Fix code generated for counted arrays X-Git-Tag: v6.16-rc1~138^2 X-Git-Url: https://git.kernel.dk/?a=commitdiff_plain;h=425364dc49f050b6008b43408aa96d42105a9c1d;p=linux-2.6-block.git xdrgen: Fix code generated for counted arrays When an XDR counted array has a maximum element count, xdrgen adds a bounds check to the encoder or decoder for that type. But in cases where the .x provides no maximum element count, such as struct notify4 { /* composed from notify_type4 or notify_deviceid_type4 */ bitmap4 notify_mask; notifylist4 notify_vals; }; struct CB_NOTIFY4args { stateid4 cna_stateid; nfs_fh4 cna_fh; notify4 cna_changes<>; }; xdrgen is supposed to omit that bounds check. Some of the Jinja2 templates handle that correctly, but a few are incorrect and leave the bounds check in place with a maximum of zero, which causes encoding/decoding of that type to fail unconditionally. Reported-by: Jeff Layton Signed-off-by: Chuck Lever --- diff --git a/tools/net/sunrpc/xdrgen/templates/C/pointer/encoder/variable_length_array.j2 b/tools/net/sunrpc/xdrgen/templates/C/pointer/encoder/variable_length_array.j2 index 0ec8660d621a..b21476629679 100644 --- a/tools/net/sunrpc/xdrgen/templates/C/pointer/encoder/variable_length_array.j2 +++ b/tools/net/sunrpc/xdrgen/templates/C/pointer/encoder/variable_length_array.j2 @@ -2,8 +2,10 @@ {% if annotate %} /* member {{ name }} (variable-length array) */ {% endif %} +{% if maxsize != "0" %} if (value->{{ name }}.count > {{ maxsize }}) return false; +{% endif %} if (xdr_stream_encode_u32(xdr, value->{{ name }}.count) != XDR_UNIT) return false; for (u32 i = 0; i < value->{{ name }}.count; i++) diff --git a/tools/net/sunrpc/xdrgen/templates/C/struct/encoder/variable_length_array.j2 b/tools/net/sunrpc/xdrgen/templates/C/struct/encoder/variable_length_array.j2 index 0ec8660d621a..b21476629679 100644 --- a/tools/net/sunrpc/xdrgen/templates/C/struct/encoder/variable_length_array.j2 +++ b/tools/net/sunrpc/xdrgen/templates/C/struct/encoder/variable_length_array.j2 @@ -2,8 +2,10 @@ {% if annotate %} /* member {{ name }} (variable-length array) */ {% endif %} +{% if maxsize != "0" %} if (value->{{ name }}.count > {{ maxsize }}) return false; +{% endif %} if (xdr_stream_encode_u32(xdr, value->{{ name }}.count) != XDR_UNIT) return false; for (u32 i = 0; i < value->{{ name }}.count; i++) diff --git a/tools/net/sunrpc/xdrgen/templates/C/union/decoder/variable_length_array.j2 b/tools/net/sunrpc/xdrgen/templates/C/union/decoder/variable_length_array.j2 index 51ad736d2530..53dfaf9cec68 100644 --- a/tools/net/sunrpc/xdrgen/templates/C/union/decoder/variable_length_array.j2 +++ b/tools/net/sunrpc/xdrgen/templates/C/union/decoder/variable_length_array.j2 @@ -4,8 +4,10 @@ {% endif %} if (xdr_stream_decode_u32(xdr, &count) < 0) return false; +{% if maxsize != "0" %} if (count > {{ maxsize }}) return false; +{% endif %} for (u32 i = 0; i < count; i++) { if (xdrgen_decode_{{ type }}(xdr, &ptr->{{ name }}.items[i]) < 0) return false;