From 3d1b0ff308effad6f941d5f5cb69ee49ef8fdc16 Mon Sep 17 00:00:00 2001
From: Joerg Wunsch <j@uriah.heep.sax.de>
Date: Fri, 12 Nov 2021 22:11:49 +0000
Subject: [PATCH] Submitted by Jon Thacker: patch #9253: Fix for giving
 terminal_mode commands more than 20 arguments * term.c (tokenize): fix
 realloc usage, pointer returned not necessarily the same as pointer passed

git-svn-id: svn://svn.savannah.nongnu.org/avrdude/trunk/avrdude@1471 81a1dc3b-b13d-400b-aceb-764788c761c2
---
 ChangeLog | 7 +++++++
 NEWS      | 1 +
 term.c    | 9 ++++++++-
 3 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/ChangeLog b/ChangeLog
index 7f42c7fb..e55724f9 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2021-11-12  Joerg Wunsch <j.gnu@uriah.heep.sax.de>
+
+	Submitted by Jon Thacker:
+	patch #9253: Fix for giving terminal_mode commands more than 20 arguments
+	* term.c (tokenize): fix realloc usage, pointer returned not necessarily
+	the same as pointer passed
+
 2021-11-12  Joerg Wunsch <j.gnu@uriah.heep.sax.de>
 
 	Submitted by Martino Facchin:
diff --git a/NEWS b/NEWS
index 791d9750..e6c91cd5 100644
--- a/NEWS
+++ b/NEWS
@@ -105,6 +105,7 @@ Current:
     patch #10017: uspasp / tpi: Automatically clear configuration byte (fuse) before writing it
     patch #8957: Allow reading prodsig memory from stk500v2 on xmega devices
     patch #9110: Let reserved fuse bits to be read as *don't care*
+    patch #9253: Fix for giving terminal_mode commands more than 20 arguments
 
   * Internals:
     - New avrdude.conf keyword "family_id", used to verify SIB attributes
diff --git a/term.c b/term.c
index f65610d3..f222fb46 100644
--- a/term.c
+++ b/term.c
@@ -780,11 +780,12 @@ static int cmd_verbose(PROGRAMMER * pgm, struct avrpart * p,
 
 static int tokenize(char * s, char *** argv)
 {
-  int     i, n, l, nargs, offset;
+  int     i, n, l, k, nargs, offset;
   int     len, slen;
   char  * buf;
   int     bufsize;
   char ** bufv;
+  char  * bufp;
   char  * q, * r;
   char  * nbuf;
   char ** av;
@@ -821,9 +822,15 @@ static int tokenize(char * s, char *** argv)
       /* realloc space for another 20 args */
       bufsize += 20;
       nargs   += 20;
+      bufp     = buf;
       buf      = realloc(buf, bufsize);
       bufv     = realloc(bufv, nargs*sizeof(char *));
       nbuf     = &buf[l];
+      /* correct bufv pointers */
+      k = buf - bufp;
+      for (i=0; i<n; i++) {
+          bufv[i] = bufv[i] + k;
+      }
       for (i=n; i<nargs; i++)
         bufv[i] = NULL;
     }