]> git.puffer.fish Git - matthieu/frr.git/commitdiff
lib: fix vtysh core when handling questionmark
authorYuan Yuan <yyuanam@amazon.com>
Tue, 30 May 2023 19:20:09 +0000 (19:20 +0000)
committerMergify <37929162+mergify[bot]@users.noreply.github.com>
Thu, 1 Jun 2023 19:31:31 +0000 (19:31 +0000)
When issue vtysh command with ?, the initial buf size for the
element is 16. Then it would loop through each element in the cmd
output vector. If the required size for printing out the next
element is larger than the current buf size, realloc the buf memory
by doubling the current buf size regardless of the actual size
that's needed. This would cause vtysh core when the doubled size
is not enough for the next element.

Signed-off-by: Yuan Yuan <yyuanam@amazon.com>
(cherry picked from commit f8aa257997a6a6f69ec5d5715ab04d7cbfae1d1c)

lib/command.c

index a23afb1e43863e15ff36c50a1d866ff1e69ca0f5..ca05cd6d2fa20d0890bd913aafc704804c7f215f 100644 (file)
@@ -743,9 +743,13 @@ char *cmd_variable_comp2str(vector comps, unsigned short cols)
                char *item = vector_slot(comps, j);
                itemlen = strlen(item);
 
-               if (cs + itemlen + AUTOCOMP_INDENT + 3 >= bsz)
-                       buf = XREALLOC(MTYPE_TMP, buf, (bsz *= 2));
+               size_t next_sz = cs + itemlen + AUTOCOMP_INDENT + 3;
 
+               if (next_sz > bsz) {
+                       /* Make sure the buf size is large enough */
+                       bsz = next_sz;
+                       buf = XREALLOC(MTYPE_TMP, buf, bsz);
+               }
                if (lc + itemlen + 1 >= cols) {
                        cs += snprintf(&buf[cs], bsz - cs, "\n%*s",
                                       AUTOCOMP_INDENT, "");