From nobody Sat May 18 05:53:12 2024 Received: from mail-wr1-f52.google.com (mail-wr1-f52.google.com [209.85.221.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 945DD15DBBA for ; Wed, 24 Apr 2024 14:05:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713967555; cv=none; b=N82H+tBbn9i2QLOui7NlKB27uUP7TYCSczaKdLzCX78374CFgSbI5zEX0PEzi2CUVZqzzC/YT78VMT+SdXI6lrI+3vg9QVocXnlPbst5zMJuz4rac9ZzlAsSwgNZoh0fIt4481LOfH3tzsWQzocRWiP4rLDkxLBhFTuCTn7TWGY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713967555; c=relaxed/simple; bh=pzM2Ee19i9xuwdVm7wtL/DpLvEo5jssFTnFEbmrlKz8=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=O98yw9KJACepSzC9e2+rUVWVgfvb+vgoIU34kHksfgblyi9zICnGQTAX8EeJnP5xXbHRv0tlZr6VRGIcRAAQbqF1q+Cz43FcD9lfcN8IzE07IQqPtJC+3Ku/NlFDhyjnQRMM8tFxL01YEBRDLe1rPja4jXiOYNSXlFzl5msJSQs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=f9XjSCXi; arc=none smtp.client-ip=209.85.221.52 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="f9XjSCXi" Received: by mail-wr1-f52.google.com with SMTP id ffacd0b85a97d-343f62d8124so5626155f8f.2 for ; Wed, 24 Apr 2024 07:05:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1713967550; x=1714572350; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=vO2qseMnEiNi51iZjrOv5zkQ/0iFN3HnTr7pOyrnOgs=; b=f9XjSCXioYNhcWvXb6pmpWn65sj9gPjjJxZDLYHaYFCFnanuvG9Hn/Av7k6YVf9o14 B5Gs0Lb/nI8HL6TIwugOUb76/wnvuJipyIMJOXgeMrDQ1/Ki8gQHD84GnvgDOuYaJWmd 9BYiSxLPdKq0GJOLsaGWbW8wSMKlHbtqeU5kQ/hCP4igE/Ti+q2412jwSrFKB2VD4yMf DdtaMco3QkQOXDHnCkaHA5jSg9hoBalo3dCa6kTxF6NHu3NgHVnHRUc9XPaxk2UNFHNx r9k6xhiGwykFKzXkkmLz4uNwPxDkOhyFEGqZ4nY+6S2CaBgtBNU0NmyUOyXJUhncDfCK Q9Aw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713967550; x=1714572350; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=vO2qseMnEiNi51iZjrOv5zkQ/0iFN3HnTr7pOyrnOgs=; b=vjbhGT0CqZKiI38oRXHkCvQOhLG8GbXq3B4Fq+TZqEVrdbfiRmjf43GRc6hlNKZtMv t8dLAd2RpU7KGM84W0ozt1yqDF2JwlTrn1SZ8e1T8f6NgpMYA7EmnsKSl2gfk5ck6Q7w C67sg42nUS5l6bBpXZYUUX3ZsfGMqCNvPdfEWI5YheemztLE4vhZI2PLjssK8Mg9RNA0 vq4EG2D10iQAXukdgkCSG69kHavMcE5ArZAEUz2ndyj3oz5MuDSmIT1fShvURJK7PKtX 8UQRYrSLLIskqt7Ddw3gCafCe2Hv4EP5R9tjLvVeuXVJbKg35NJthpyJJ2HlvaLLUxpb tDcA== X-Forwarded-Encrypted: i=1; AJvYcCWHCizikdAoDz+fpuzhQK520j3Ls/O2EdHZfgG4rGLmJk9OpZv4neN1FQRTHFZPeVMcu7CiaPiR1SwFbVSQCBQBpnxA7xjb0ku8ZfGd X-Gm-Message-State: AOJu0Ywpbtf7rD29OeRsfkZBiCFEgWHJXx13owIL0AfpPL4gTtfvjTbi laMKJt0wfTGsnJybIyPU/glb+f7kkpgn2/tsg5psas9caqKCZDZF6ZStKbVJre2BnHZTLkikKlj m4cw= X-Google-Smtp-Source: AGHT+IG4FtWF4GLSRqwunMp9Dp7ZmNUSmYCWY3Ci5BIcaqIjosU7J9HsN8aw6mTgzIHb0p4bdhMnoA== X-Received: by 2002:a5d:408d:0:b0:34b:b0ac:c63c with SMTP id o13-20020a5d408d000000b0034bb0acc63cmr1766527wrp.66.1713967549826; Wed, 24 Apr 2024 07:05:49 -0700 (PDT) Received: from aspen.lan (aztw-34-b2-v4wan-166919-cust780.vm26.cable.virginm.net. [82.37.195.13]) by smtp.gmail.com with ESMTPSA id p8-20020a5d48c8000000b0034af40b2efdsm9105325wrs.108.2024.04.24.07.05.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Apr 2024 07:05:49 -0700 (PDT) From: Daniel Thompson Date: Wed, 24 Apr 2024 15:03:34 +0100 Subject: [PATCH v3 1/7] kdb: Fix buffer overflow during tab-complete Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20240424-kgdb_read_refactor-v3-1-f236dbe9828d@linaro.org> References: <20240424-kgdb_read_refactor-v3-0-f236dbe9828d@linaro.org> In-Reply-To: <20240424-kgdb_read_refactor-v3-0-f236dbe9828d@linaro.org> To: Jason Wessel , Douglas Anderson Cc: kgdb-bugreport@lists.sourceforge.net, linux-kernel@vger.kernel.org, Daniel Thompson , Justin Stitt , stable@vger.kernel.org X-Mailer: b4 0.13.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2263; i=daniel.thompson@linaro.org; h=from:subject:message-id; bh=pzM2Ee19i9xuwdVm7wtL/DpLvEo5jssFTnFEbmrlKz8=; b=owEBbQKS/ZANAwAKAXzjJV0594ihAcsmYgBmKRFeBl7WDOxk6g2Vz1ghh35u91oebBU7XTmKb eGAWsdXcFWJAjMEAAEKAB0WIQQvNUFTUPeVarpwrPB84yVdOfeIoQUCZikRXgAKCRB84yVdOfeI odcZD/wNGGoEPbgv3vkcFidVOxzNb+ZDeswc+rrXpA9em1dPd7pWJyJNNjW7V8dH2A8vNlyRzJM nHjNW2jzFgd1Kci6i5IXSAY9WtbRlMIzwW05tyCPX7Y90JriEepDE+rnPFV/mgl4OKYlihJ/4w4 QzrPADJ6x9Q/+WKg1hu9sQBhT9msg1JS1ruCiAMzSG6eU2W8r2eNtKHlBiyDa49Bi6pMip9Igdg sblCHRpsLmWF6wO8YZMqToE3jjp/tbjlOykkiuAE2djgVEUHgxLTAmpVYVObwf1AfJZlhEbTgFb m14g4bV8FPfvje0AoLb4lwM/s0nngV5d+ddUUn9jUJMi/Xq7xeyW81YO7Cz1cdupY2RVCbHe7vP tzzMuN7K3fH7ZOutVmV0Z6ooubU/03li+u7kfFKp6v5BHvA4aG0xB7p0j9k3S80ibxUxMkrLyHV qGcQp5fZBnqYnkXqm/xrK8nnoT1FA661h7QTND/hs7EFEwLP8iEQD1+xIr0vxLhOCicvTfCZUlI zuVf7WmSFiXv5jHc6OKgxf9CLpQMifkADPqziUDKIVV3yTM8WI868/GEI/zy7TxO32r/5q/pceM Es4HFumgKjjajRixUEuTVJoJD1ezkci4ccm5uaQGVLxmmExhSd4Gm6PF0o8V2zQc7x9goo12Dgy DNtXUsXfO58HZYw== X-Developer-Key: i=daniel.thompson@linaro.org; a=openpgp; fpr=E38BE19861669213F6E2661AA8A4E3BC5B7B28BE Currently, when the user attempts symbol completion with the Tab key, kdb will use strncpy() to insert the completed symbol into the command buffer. Unfortunately it passes the size of the source buffer rather than the destination to strncpy() with predictably horrible results. Most obviously if the command buffer is already full but cp, the cursor position, is in the middle of the buffer, then we will write past the end of the supplied buffer. Fix this by replacing the dubious strncpy() calls with memmove()/memcpy() calls plus explicit boundary checks to make sure we have enough space before we start moving characters around. Reported-by: Justin Stitt Closes: https://lore.kernel.org/all/CAFhGd8qESuuifuHsNjFPR-Va3P80bxrw+LqvC8= deA8GziUJLpw@mail.gmail.com/ Cc: stable@vger.kernel.org Reviewed-by: Douglas Anderson Reviewed-by: Justin Stitt Tested-by: Justin Stitt Signed-off-by: Daniel Thompson --- kernel/debug/kdb/kdb_io.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/kernel/debug/kdb/kdb_io.c b/kernel/debug/kdb/kdb_io.c index 9443bc63c5a24..06dfbccb10336 100644 --- a/kernel/debug/kdb/kdb_io.c +++ b/kernel/debug/kdb/kdb_io.c @@ -367,14 +367,19 @@ static char *kdb_read(char *buffer, size_t bufsize) kdb_printf(kdb_prompt_str); kdb_printf("%s", buffer); } else if (tab !=3D 2 && count > 0) { - len_tmp =3D strlen(p_tmp); - strncpy(p_tmp+len_tmp, cp, lastchar-cp+1); - len_tmp =3D strlen(p_tmp); - strncpy(cp, p_tmp+len, len_tmp-len + 1); - len =3D len_tmp - len; - kdb_printf("%s", cp); - cp +=3D len; - lastchar +=3D len; + /* How many new characters do we want from tmpbuffer? */ + len_tmp =3D strlen(p_tmp) - len; + if (lastchar + len_tmp >=3D bufend) + len_tmp =3D bufend - lastchar; + + if (len_tmp) { + /* + 1 ensures the '\0' is memmove'd */ + memmove(cp+len_tmp, cp, (lastchar-cp) + 1); + memcpy(cp, p_tmp+len, len_tmp); + kdb_printf("%s", cp); + cp +=3D len_tmp; + lastchar +=3D len_tmp; + } } kdb_nextline =3D 1; /* reset output line number */ break; --=20 2.43.0 From nobody Sat May 18 05:53:12 2024 Received: from mail-wm1-f51.google.com (mail-wm1-f51.google.com [209.85.128.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 57381143878 for ; Wed, 24 Apr 2024 14:05:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713967556; cv=none; b=Ju8ypGWmXv9/aVnLzMc+c6q4yFXMyiD+3c3B+cnks0bfAQAh5QvchBdIdzqJ8ESw38SXuSQs+IDu/NhKGizVBvXI0OpofDBzyJEqZdDF4CelHeU0yV8HvFPxNBRVRycjrL/lJvw0tvPw4suPJ48KJXLHMevS/N7+I5QD2kGXLsE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713967556; c=relaxed/simple; bh=pUuir1uu0AsFezKSVWCJ+HLjIqUcc98HfYuUzHwdnik=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=S6bAKEcFbhb8Z3AOBW2f67gk9HPbjJkWMgwsGIpuDmmkJoAnN6hP0eZ6ct/7f9I9he1xjcb+G2L2SPCgSwTswcyqa1w8rpC4ftv5XGIWHUQ+N+8pz0d2SI+4UMgzLrHVjgvGo6fwgNdrNU1F3hXV1qfDRbuyLNwTwTT94PPRLgA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=JN4K+gAM; arc=none smtp.client-ip=209.85.128.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="JN4K+gAM" Received: by mail-wm1-f51.google.com with SMTP id 5b1f17b1804b1-41b2119da94so2169485e9.0 for ; Wed, 24 Apr 2024 07:05:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1713967552; x=1714572352; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=DpHsNOkxngtvba9wSq++zmIn8As88KCaI91fFzXimkg=; b=JN4K+gAMMB5YauThDrEgmuzfGxUTGN0YegMTcLohBstHKK/I+vMMkgiKAtKUYmQ0v/ BVR8xcCSjAIFr8xxqB/kXJjGsltaDHxDf958ezFHiibWjeTbFr86k327h5c9ZZa2dzl3 FEOjTA2aBKmnYC9FA07wAZGiI3PgYB9htUYlw3aBXwjvyOHxFbnn60nlSB9IRqNtAekf MId+ZDKCOillGzcwZKwN54m3XeS7uLL3LKVWrp2K73hO9MRBcobgL3TBSpK/DKzUY1mu AIZhzH4gVSSdqS6rgZmIZNQhlYsaU0WS+5EVzy4Odv6aLromF0rbPVr1iaMugjIca2Hr Q3QA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713967552; x=1714572352; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=DpHsNOkxngtvba9wSq++zmIn8As88KCaI91fFzXimkg=; b=NatQDI6g9ghGlYmB43sA0jqQTQTanQiRXNgbGnIJIJd/ZCihDVPPbp2vaazHPugUMo mlXfxy9ihbxvYGfxxnS2rXGB6tFJoA4F8Ffz09P56hIkWiZl2Qid0Q4samPlkS/Ol6eB IkttJ1qGhr4+jwB+KvEzA1iVg6iKTRjqf9Gt4NodF0t+4zjXG2vDEJ0K4ABTolexiOAc hfEcsFOsoev8HgA0FlwZEAOaZVZfwry98ZfbOl8oWjoH21fdv9DALwQU1bGfiHeQBQsB YB/oPrLXeMgBC9Ba4yKyhL6JHvgIgjkP4XLr6pObOqM5h2mmfmEXOwVONin+8wqdV3Al Aw4A== X-Forwarded-Encrypted: i=1; AJvYcCWTD+L7G/YxsNYPh5Lyy3W5aVS+C1Q+wUkZu0FFLj1LjZ0v2lIpgCq5L5y5rnHh63Ld68mu+GY3eiBo5Z7SfmUoipTENfLil+rML43m X-Gm-Message-State: AOJu0YwdV/jjuNdPFY1f0YptGOT+70umiXWfRR7ubZ5xhUW2m4QNVOPb uXD+GKdiNFrHR7cs370uivyxXkRoMVYtHNJL2xXMRrrpxY9zg2ZbDv3SWHz39bDYTCwog8yfJql EUjk= X-Google-Smtp-Source: AGHT+IHrPrgSzW/eVw7jFZHVPCK2stE2i1eZs6gOPOq9XnTtiiffxLzlqTup/NIJDSvunaQRTGqq7w== X-Received: by 2002:a5d:574a:0:b0:34a:e73a:56a3 with SMTP id q10-20020a5d574a000000b0034ae73a56a3mr1627330wrw.7.1713967551890; Wed, 24 Apr 2024 07:05:51 -0700 (PDT) Received: from aspen.lan (aztw-34-b2-v4wan-166919-cust780.vm26.cable.virginm.net. [82.37.195.13]) by smtp.gmail.com with ESMTPSA id p8-20020a5d48c8000000b0034af40b2efdsm9105325wrs.108.2024.04.24.07.05.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Apr 2024 07:05:50 -0700 (PDT) From: Daniel Thompson Date: Wed, 24 Apr 2024 15:03:35 +0100 Subject: [PATCH v3 2/7] kdb: Use format-strings rather than '\0' injection in kdb_read() Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20240424-kgdb_read_refactor-v3-2-f236dbe9828d@linaro.org> References: <20240424-kgdb_read_refactor-v3-0-f236dbe9828d@linaro.org> In-Reply-To: <20240424-kgdb_read_refactor-v3-0-f236dbe9828d@linaro.org> To: Jason Wessel , Douglas Anderson Cc: kgdb-bugreport@lists.sourceforge.net, linux-kernel@vger.kernel.org, Daniel Thompson , stable@vger.kernel.org, Justin Stitt X-Mailer: b4 0.13.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=4021; i=daniel.thompson@linaro.org; h=from:subject:message-id; bh=pUuir1uu0AsFezKSVWCJ+HLjIqUcc98HfYuUzHwdnik=; b=owEBbQKS/ZANAwAKAXzjJV0594ihAcsmYgBmKRFrh6HPOcCklaq0yycGzE/JCVXYmszt3a2b3 ez4yq9phveJAjMEAAEKAB0WIQQvNUFTUPeVarpwrPB84yVdOfeIoQUCZikRawAKCRB84yVdOfeI oSo0D/0ap+RFd7GQkBARE2EvV7WKwb/s+o03JffFP+H5p7ja6NtXnZxw2vCbIgxiEa+SiqrfVGg yzaB8XzeG7cYtdaPgecedLfh5K1TbOsl+XwTvaLmIGC5Xtm607+HG+lGv327PjwGX+0pFns5tOM wRUYTWBHJel5K4vQbkNlCGBEiofmusoJysKAVhfDZ7lV23MoO5IYJ2LK+1et6dyHVcadeAYRqkS jpgJN81La4V6lDejD5wttUylt2l7nxsrjvVcydVhXPGxY7ZWgZRHC05DzLJePM+hQKANEeUDaqh mOyeA0tZKEUMftyKX3C/iwVUoSEvjJrgdgNC930zDugN162bUIj+yz2rqXjNmCZHh+rjEuYNDKZ 5zlUw61uA1i3xJQifNTedO1YL0lS/+IurpfQ+XAiGBzUC2QadU5uA4OPk3ehLGWmo1AXEZ70ozV jMHZYX5KxbecN4V9KZh8p1D6cahJN2HLZ5oOeIp3jjv0GD3Bl+QQo2mIq1JRmfyliCm76mm3VRB j5hqskDS1vzo9kahcpwK4TPVe5R0Y1iaA+K86culQYQ2FSDbywkV9L3WGZvpUFCsMpVoqD5A59x Lv+9LXP2hbo4wVaObNRyx6vwMVhzAeXQjJKrF8pcn8kb6Q/Zp6wh65Gv0yoQ0kzy6R3Ej0Fib/h bqSnTUTmJdvcI6Q== X-Developer-Key: i=daniel.thompson@linaro.org; a=openpgp; fpr=E38BE19861669213F6E2661AA8A4E3BC5B7B28BE Currently when kdb_read() needs to reposition the cursor it uses copy and paste code that works by injecting an '\0' at the cursor position before delivering a carriage-return and reprinting the line (which stops at the '\0'). Tidy up the code by hoisting the copy and paste code into an appropriately named function. Additionally let's replace the '\0' injection with a proper field width parameter so that the string will be abridged during formatting instead. Cc: stable@vger.kernel.org # Not a bug fix but it is needed for later bug f= ixes Tested-by: Justin Stitt Reviewed-by: Douglas Anderson Signed-off-by: Daniel Thompson --- kernel/debug/kdb/kdb_io.c | 55 +++++++++++++++++++++++++++++--------------= ---- 1 file changed, 34 insertions(+), 21 deletions(-) diff --git a/kernel/debug/kdb/kdb_io.c b/kernel/debug/kdb/kdb_io.c index 06dfbccb10336..50789c99b3ba8 100644 --- a/kernel/debug/kdb/kdb_io.c +++ b/kernel/debug/kdb/kdb_io.c @@ -184,6 +184,33 @@ char kdb_getchar(void) unreachable(); } =20 +/** + * kdb_position_cursor() - Place cursor in the correct horizontal position + * @prompt: Nil-terminated string containing the prompt string + * @buffer: Nil-terminated string containing the entire command line + * @cp: Cursor position, pointer the character in buffer where the cursor + * should be positioned. + * + * The cursor is positioned by sending a carriage-return and then printing + * the content of the line until we reach the correct cursor position. + * + * There is some additional fine detail here. + * + * Firstly, even though kdb_printf() will correctly format zero-width fiel= ds + * we want the second call to kdb_printf() to be conditional. That keeps t= hings + * a little cleaner when LOGGING=3D1. + * + * Secondly, we can't combine everything into one call to kdb_printf() sin= ce + * that renders into a fixed length buffer and the combined print could re= sult + * in unwanted truncation. + */ +static void kdb_position_cursor(char *prompt, char *buffer, char *cp) +{ + kdb_printf("\r%s", kdb_prompt_str); + if (cp > buffer) + kdb_printf("%.*s", (int)(cp - buffer), buffer); +} + /* * kdb_read * @@ -212,7 +239,6 @@ static char *kdb_read(char *buffer, size_t bufsize) * and null byte */ char *lastchar; char *p_tmp; - char tmp; static char tmpbuffer[CMD_BUFLEN]; int len =3D strlen(buffer); int len_tmp; @@ -249,12 +275,8 @@ static char *kdb_read(char *buffer, size_t bufsize) } *(--lastchar) =3D '\0'; --cp; - kdb_printf("\b%s \r", cp); - tmp =3D *cp; - *cp =3D '\0'; - kdb_printf(kdb_prompt_str); - kdb_printf("%s", buffer); - *cp =3D tmp; + kdb_printf("\b%s ", cp); + kdb_position_cursor(kdb_prompt_str, buffer, cp); } break; case 10: /* linefeed */ @@ -272,19 +294,14 @@ static char *kdb_read(char *buffer, size_t bufsize) memcpy(tmpbuffer, cp+1, lastchar - cp - 1); memcpy(cp, tmpbuffer, lastchar - cp - 1); *(--lastchar) =3D '\0'; - kdb_printf("%s \r", cp); - tmp =3D *cp; - *cp =3D '\0'; - kdb_printf(kdb_prompt_str); - kdb_printf("%s", buffer); - *cp =3D tmp; + kdb_printf("%s ", cp); + kdb_position_cursor(kdb_prompt_str, buffer, cp); } break; case 1: /* Home */ if (cp > buffer) { - kdb_printf("\r"); - kdb_printf(kdb_prompt_str); cp =3D buffer; + kdb_position_cursor(kdb_prompt_str, buffer, cp); } break; case 5: /* End */ @@ -390,13 +407,9 @@ static char *kdb_read(char *buffer, size_t bufsize) memcpy(cp+1, tmpbuffer, lastchar - cp); *++lastchar =3D '\0'; *cp =3D key; - kdb_printf("%s\r", cp); + kdb_printf("%s", cp); ++cp; - tmp =3D *cp; - *cp =3D '\0'; - kdb_printf(kdb_prompt_str); - kdb_printf("%s", buffer); - *cp =3D tmp; + kdb_position_cursor(kdb_prompt_str, buffer, cp); } else { *++lastchar =3D '\0'; *cp++ =3D key; --=20 2.43.0 From nobody Sat May 18 05:53:12 2024 Received: from mail-wm1-f50.google.com (mail-wm1-f50.google.com [209.85.128.50]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 119C015E209 for ; Wed, 24 Apr 2024 14:05:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.50 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713967559; cv=none; b=IAjOyo3gjqRql8GUfN1HlTQV0mDuFxImBI2eod9fA/zyBshM5Qu6LlwBAcgmHA1xZvv7rK92cg6R/7LM3TxW+z2R5BXZ5HFeVehVk7n4zJvKFdgx4ZJQPTUyY0gstmZ1r3iwLdW/Xm7yo1s/QBGfkdly+6gwAMiRo9h7jx1c54g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713967559; c=relaxed/simple; bh=wti5kj6bWe28mBREcjkcFgjgGXuk8vkEPAf7amsQIJY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=AoNRTzdMZA9HMPO/y5ajB+bSX9wmdDHHNjfPTWP2uAtII9s3N+7+gzRp9+B85dN6NSbKVIdwpqu1n3ta/uXOcbmY1K/6HfwCqR5bS6CZfKSfM1eBzEHhKr2xi3G1jXtBSE7iQObK4xGP969hoyRW9RacWGU897xXL/+KROlJqac= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=IcMlWtTp; arc=none smtp.client-ip=209.85.128.50 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="IcMlWtTp" Received: by mail-wm1-f50.google.com with SMTP id 5b1f17b1804b1-41b21ed19f5so2119135e9.2 for ; Wed, 24 Apr 2024 07:05:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1713967553; x=1714572353; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=CiQSgCwG9Fjd2pIkvI/eNn2TXAIZ8kV9PyuFCdDbFAA=; b=IcMlWtTpKNdt7yNtjhY9H/vg/0adY31kwIUKmZvtvLiuCbxMkFr4eRGr5+zShWspq5 EGm22IODuDxXRWfeU/Bsh5vvjMgQsMl7KO7AYo0YYZI3LGCtp/L4Nhxks4RreQIXoWT3 6qp47XX5Q9zbim1KyL6jTZc9lBQo7QFWBRSxdzFTh5MVX7TPOeN0C2D/QkS3Lqew+63+ a1TUOhqUketsJh47MSqgi3hQtbp7mSIpsE2Lgn5cwvKSLhtkxD9DjqThLCl+CP6gZD96 Nuqwzt2JikgJP3TU6APs6ASpPiw5gr1BlFrCi/qnRaRc+Z16sg1TXyPRPkv+MbFfsAdb aZRw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713967553; x=1714572353; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=CiQSgCwG9Fjd2pIkvI/eNn2TXAIZ8kV9PyuFCdDbFAA=; b=UCni7oIPP1qiDxCXAMcoQHanHX7CpBoeuzYteYEc0+Qx792tjEOk05cBo98CPnh7FN /VdVV1Nyfye6TLTpgKPtnIWjaLkPidUwvNOBOBZPH9KWPTl6ds1MYgIhY/nfxvDhno00 6rUOSH2Ix5vixkDSMKmGbqIZFlgluW/CBEHchqjOTjN1EsLdzixv+caQNgcr5iXcH2a3 AKKaQ8b8PA5qyMKz+G8xfqni5WBw/x1YOYPVGvZWVTLN+Y9o/JrXCULnNxWRTcRuNKSt RP4u0xOURsSh76NI7+V7huk8/QqT1F1CH/cnVWvT0W9n+egeg3AIImYgm9KBWPGS+oJ9 whcw== X-Forwarded-Encrypted: i=1; AJvYcCXv+V1FbozuWVTmXEutFUkg46LBB6/8LzDi2TIGQocNh4ZqMhoGdgw84nbbqNKbhkPaY2wOXnrfvhAnFYP+ePXCB2X2GqRqNmkT3LmV X-Gm-Message-State: AOJu0Yy4NSIJaCffCQnqOD14qHcSF33M68GCtirFbLkhdYwIkw/73/72 i6tLXaaka3OlCkVv3k8f5sVX4iInCghPwi6qeJrYRPaRNRuK+hcjU3KZ3IkVJSQDejf1Hb115FR cl6s= X-Google-Smtp-Source: AGHT+IGqG2RVeoMAdeMDYTn9tNZggqfDQ+qae44PwKczUO0hg8teOGcUdrRa+KCXGTu719wfKa2arQ== X-Received: by 2002:a5d:5449:0:b0:34a:a836:b940 with SMTP id w9-20020a5d5449000000b0034aa836b940mr1496105wrv.18.1713967553671; Wed, 24 Apr 2024 07:05:53 -0700 (PDT) Received: from aspen.lan (aztw-34-b2-v4wan-166919-cust780.vm26.cable.virginm.net. [82.37.195.13]) by smtp.gmail.com with ESMTPSA id p8-20020a5d48c8000000b0034af40b2efdsm9105325wrs.108.2024.04.24.07.05.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Apr 2024 07:05:52 -0700 (PDT) From: Daniel Thompson Date: Wed, 24 Apr 2024 15:03:36 +0100 Subject: [PATCH v3 3/7] kdb: Fix console handling when editing and tab-completing commands Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20240424-kgdb_read_refactor-v3-3-f236dbe9828d@linaro.org> References: <20240424-kgdb_read_refactor-v3-0-f236dbe9828d@linaro.org> In-Reply-To: <20240424-kgdb_read_refactor-v3-0-f236dbe9828d@linaro.org> To: Jason Wessel , Douglas Anderson Cc: kgdb-bugreport@lists.sourceforge.net, linux-kernel@vger.kernel.org, Daniel Thompson , stable@vger.kernel.org, Justin Stitt X-Mailer: b4 0.13.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=1858; i=daniel.thompson@linaro.org; h=from:subject:message-id; bh=wti5kj6bWe28mBREcjkcFgjgGXuk8vkEPAf7amsQIJY=; b=owEBbQKS/ZANAwAKAXzjJV0594ihAcsmYgBmKRF5rcqN5Dgc+qstFjiPAW9G3hYBgyjtuKx/O swzPOkiGi2JAjMEAAEKAB0WIQQvNUFTUPeVarpwrPB84yVdOfeIoQUCZikReQAKCRB84yVdOfeI oZjqD/43oGbEO2TFmRq3wSCHWMsq9Im7d0XzuP6Xg6OkzQSMPMsg/HnNUiCk/wDXM00xOeKEbii 4cU4AETV3oDjp6xBzEveKXrybeWohUWm5Xw4A7NBghLtRIxQZiqPo1K5ZFYwEx9wJ0H5bV9i8V+ 5+40DrU28YsTTricP5jKJzKqTQX+JRuurE8td8GTROh/0HElI0ivzakG7tSJJd5yzM+FFiTJTin sVuW+imTw06DPAVR5SlUiBPtWOQ/maUdfNmCf1HDz9iqG35YH58o4ld3cn/4WiC4GAEfEQQZKn6 uLMK5tZGYB7/AjkgsKuxbpGhUCzv9U2toh+zg9Uk5RcJnDDm/GXuYPOFZiEpVErNH+2JFt8CBJm n8l8XydEaaFvPUgXY6/wJZ1R5R4FqOsosP9RRdIZK7g8SGek6RUebPV7T+RE7FA3n7uEbdQwctI 7K91q6CqDsJaGD1PeCm/vCiLKC9mNv66KCVzCK0FDtUfjLP8zLTGYuYbCoKbqY/BRYY376R17Vz SYuypvrzNgSz0GVneF5COWavX9vMDvfIaDaYcjiALtgCIIHM4NKgDSzBZsX52dflKFeUzDBPWy1 5sb1DLSBpau+aa0r7C0Mb+MBz/JAMUMyVK4PXUP9PAfDSgAD6nHQEfoQbizf/PhzwHm+nkgTsJ5 fAV1buYnpy4dfLw== X-Developer-Key: i=daniel.thompson@linaro.org; a=openpgp; fpr=E38BE19861669213F6E2661AA8A4E3BC5B7B28BE Currently, if the cursor position is not at the end of the command buffer and the user uses the Tab-complete functions, then the console does not leave the cursor in the correct position. For example consider the following buffer with the cursor positioned at the ^: md kdb_pro 10 ^ Pressing tab should result in: md kdb_prompt_str 10 ^ However this does not happen. Instead the cursor is placed at the end (after then 10) and further cursor movement redraws incorrectly. The same problem exists when we double-Tab but in a different part of the code. Fix this by sending a carriage return and then redisplaying the text to the left of the cursor. Cc: stable@vger.kernel.org Reviewed-by: Douglas Anderson Tested-by: Justin Stitt Signed-off-by: Daniel Thompson --- kernel/debug/kdb/kdb_io.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/kernel/debug/kdb/kdb_io.c b/kernel/debug/kdb/kdb_io.c index 50789c99b3ba8..5fccb46f399e5 100644 --- a/kernel/debug/kdb/kdb_io.c +++ b/kernel/debug/kdb/kdb_io.c @@ -383,6 +383,8 @@ static char *kdb_read(char *buffer, size_t bufsize) kdb_printf("\n"); kdb_printf(kdb_prompt_str); kdb_printf("%s", buffer); + if (cp !=3D lastchar) + kdb_position_cursor(kdb_prompt_str, buffer, cp); } else if (tab !=3D 2 && count > 0) { /* How many new characters do we want from tmpbuffer? */ len_tmp =3D strlen(p_tmp) - len; @@ -396,6 +398,9 @@ static char *kdb_read(char *buffer, size_t bufsize) kdb_printf("%s", cp); cp +=3D len_tmp; lastchar +=3D len_tmp; + if (cp !=3D lastchar) + kdb_position_cursor(kdb_prompt_str, + buffer, cp); } } kdb_nextline =3D 1; /* reset output line number */ --=20 2.43.0 From nobody Sat May 18 05:53:12 2024 Received: from mail-wr1-f54.google.com (mail-wr1-f54.google.com [209.85.221.54]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2822215E7E3 for ; Wed, 24 Apr 2024 14:05:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713967560; cv=none; b=cvFgqTA2svkfNn8eCReTKndoo4qdm/bI0WSoKlw+4TdZSioBwe2uJnOjA9jIJsSrfEBmcj7bny44NUM5v8HNKLzUxijjPeNeprcSDqGj1tUFai+3UDvZwOUQoNa4V6NEARA5QSG8sGVJQ6TJBLSoFNGg3DvZjBxe0xi8H8Si7XI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713967560; c=relaxed/simple; bh=rg+wVmfYkRGv9/2I6H3Tms0eU7TpUVK8/Yo0BHNg5Cg=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=PJmamJ0NzQLPPuMr6CrC+Y5p7mg4jr9sq8vojdVZfdSqv1Z/7aCmhBuLgqJbLIH4ik57+TlKOX5sVnpi8V4XqreLVjB5QgwPl6mYvL5c+w7OlCE/v48kQ3fMa8CjlW5Tn9Myn1ch4FqYLNBJtodPPrlag50Sn3Sd/mEt1rji5rw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=HBhJP5OV; arc=none smtp.client-ip=209.85.221.54 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="HBhJP5OV" Received: by mail-wr1-f54.google.com with SMTP id ffacd0b85a97d-345b857d7adso5510238f8f.1 for ; Wed, 24 Apr 2024 07:05:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1713967555; x=1714572355; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=wTP9sloie88dSAOWvNZGscW0v0fJi60UyTt3HwL6Nts=; b=HBhJP5OVzkHV6Z7uLrmCiQSRHuSuwwQUFMBbgjr4CMm0Z5gLxlDN68xmgH+pP7kjEx 55+qsxC8ad2HYjEF8gBga0NgWFHH2JhF/9qC+NpOC1tsb5NGkDohu/7amhZ8t/5KqCcX apF7qBQrfQ2co3kNWWo+bcYiaDrLLNP45DjPWRszmaGQo29PXyA9yBsjXnj57OryCNot Cp+iCDTTAzVTTyhm26WOZddMdb42k6VTQ7rvGwrdcvJtqVX80xHFaJRG3TrYonYtuDXV M0wseJULGQmhMtPw6ZwQdXUPtv3xHAX6+OfRmaPgYwVUNP4qkgGl/DgP2n/iXwJa/dcQ y0zw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713967555; x=1714572355; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=wTP9sloie88dSAOWvNZGscW0v0fJi60UyTt3HwL6Nts=; b=mFi9KSns56wcknFg7kszAmxKC71JEWxdNez1G6Munh9NDZWxPv+giKpfuA04kSjDck 9cnm64tJaqvu3rvrrI0wjRDkk5AqJFOYC6w0nTXin8EZV2zCfbiVCNo8T83TtbE7Fakk hHqMK6B723bYf6zLTqoNWVOacH6NP/mBDVc+GaGiB7MyGpHzjHb3aVpJD8RUvbDBetNc LpCwGKmYJ+NPJ3+eShTUG8/6xYtTigCiBFAwPc/vgL6K32u97UjCcRhfpEsLrHays6Sx 9IjuWv3P4ZyfHNCk0LO3yJtmHCEnUGCB+74u8i/HWIv5r2R/86z/PZJNRMPryl7TwQZ4 Dekg== X-Forwarded-Encrypted: i=1; AJvYcCWHsOfpg4DcJtzdE9nEjJRKOEH7AtE0MbDCDubALzOGrKTQZANG9yhZtNfxUikYWbhQjGwNddUrS5iP0zA81llP3qe8tuAwi3Ruljps X-Gm-Message-State: AOJu0Yzn0lWo7HkND0FFhJvB07V3QXCsV72F553CWnQs/MvOb8BVO8le QX0+EC1bkgqbVo+2uV5+Nr9avwdJYFZmbsDAoc89nztWly9sdNH16XR5K8FbTGTCAmMNnbv77Dk bIKA= X-Google-Smtp-Source: AGHT+IEPX+Xxot+imSpOcgmGXIlR2aQ/wmTSkNDbehn8kMSExZwzE+2IRDZ83atLmZU3x+oYV22avw== X-Received: by 2002:adf:fd05:0:b0:347:70ce:acbe with SMTP id e5-20020adffd05000000b0034770ceacbemr1867171wrr.67.1713967555393; Wed, 24 Apr 2024 07:05:55 -0700 (PDT) Received: from aspen.lan (aztw-34-b2-v4wan-166919-cust780.vm26.cable.virginm.net. [82.37.195.13]) by smtp.gmail.com with ESMTPSA id p8-20020a5d48c8000000b0034af40b2efdsm9105325wrs.108.2024.04.24.07.05.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Apr 2024 07:05:54 -0700 (PDT) From: Daniel Thompson Date: Wed, 24 Apr 2024 15:03:37 +0100 Subject: [PATCH v3 4/7] kdb: Merge identical case statements in kdb_read() Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20240424-kgdb_read_refactor-v3-4-f236dbe9828d@linaro.org> References: <20240424-kgdb_read_refactor-v3-0-f236dbe9828d@linaro.org> In-Reply-To: <20240424-kgdb_read_refactor-v3-0-f236dbe9828d@linaro.org> To: Jason Wessel , Douglas Anderson Cc: kgdb-bugreport@lists.sourceforge.net, linux-kernel@vger.kernel.org, Daniel Thompson , stable@vger.kernel.org, Justin Stitt X-Mailer: b4 0.13.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=1374; i=daniel.thompson@linaro.org; h=from:subject:message-id; bh=rg+wVmfYkRGv9/2I6H3Tms0eU7TpUVK8/Yo0BHNg5Cg=; b=owEBbQKS/ZANAwAKAXzjJV0594ihAcsmYgBmKRGGUIqVKrOdXwYeS3cqs6ybpiHvyFYQnDNDX wKlZXU4yfOJAjMEAAEKAB0WIQQvNUFTUPeVarpwrPB84yVdOfeIoQUCZikRhgAKCRB84yVdOfeI obU+EACP6jy9g//wnAmPJ7pvsIGCksOaTaiCmuiV6HeoRkqrYb/8LXKinX2ulqQtBlzx/m3gj/1 r903vaASLGgIfcByimnXKvcBwOWOKSCtf7hg+Pdz3H6UPCSHLi09U4EjXe4Z0Q7KmX+rpHHt4Rm cXCJDQw6Oi6GThI2/YK4ZU2AXlxUwH7EscpSbEJSDljvRzqTkCFIGUo/NhMhpAyB2MEK+GuftBT CcrpGKerGJGD9X/I/YPLD1r8PMxTXMPSiWJpJWttiy45HQiX2ziL42syJibH2kb84KjzQbgXOt6 0puhYjeHPcK1L14d051++9Cp+bdKdhPuDLY6X5wjKHTY2rFW06Ms0wwIzE/QIrXwT+t3J6KKW+w 66Zxi6Bm6O15lrO5AlEXAQL7eCNIMWmIuUHWXP3iihytoXvwViLOWQ6A5BVeG5cu+F/r95czqdj QYiOJ8A06T6q2kI/f7WVIh+NU0AUJSQu0Pna52M+mJHAmymxRQUDrs/aDXncVh/pRgS6hiBIqpq 36eHXGO6Ykh7SoLQRVzmkpbbKQ+aXLMcL0PAy0GdvDJGYqcvWtRxEXmSrfF0rwYJnBKhuXA0Ah5 gAqwsPthBijNpxOESrNb7XMHsUvEaDHlcuzeKwkpcTO7wTFuiOB7GfjdTniKGz9JXyoDCmVisXR sF37CLSCUj/j3YA== X-Developer-Key: i=daniel.thompson@linaro.org; a=openpgp; fpr=E38BE19861669213F6E2661AA8A4E3BC5B7B28BE The code that handles case 14 (down) and case 16 (up) has been copy and pasted despite being byte-for-byte identical. Combine them. Cc: stable@vger.kernel.org # Not a bug fix but it is needed for later bug f= ixes Reviewed-by: Douglas Anderson Tested-by: Justin Stitt Signed-off-by: Daniel Thompson --- kernel/debug/kdb/kdb_io.c | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/kernel/debug/kdb/kdb_io.c b/kernel/debug/kdb/kdb_io.c index 5fccb46f399e5..a73779529803f 100644 --- a/kernel/debug/kdb/kdb_io.c +++ b/kernel/debug/kdb/kdb_io.c @@ -317,6 +317,7 @@ static char *kdb_read(char *buffer, size_t bufsize) } break; case 14: /* Down */ + case 16: /* Up */ memset(tmpbuffer, ' ', strlen(kdb_prompt_str) + (lastchar-buffer)); *(tmpbuffer+strlen(kdb_prompt_str) + @@ -331,15 +332,6 @@ static char *kdb_read(char *buffer, size_t bufsize) ++cp; } break; - case 16: /* Up */ - memset(tmpbuffer, ' ', - strlen(kdb_prompt_str) + (lastchar-buffer)); - *(tmpbuffer+strlen(kdb_prompt_str) + - (lastchar-buffer)) =3D '\0'; - kdb_printf("\r%s\r", tmpbuffer); - *lastchar =3D (char)key; - *(lastchar+1) =3D '\0'; - return lastchar; case 9: /* Tab */ if (tab < 2) ++tab; --=20 2.43.0 From nobody Sat May 18 05:53:12 2024 Received: from mail-wr1-f43.google.com (mail-wr1-f43.google.com [209.85.221.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F3AEC15ECD5 for ; Wed, 24 Apr 2024 14:05:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713967563; cv=none; b=bBszgRQOUhMIFLe1TKnSa1UP2cB4xTttlyDsX1yJnM4L/LHpif0Yl/hfXi8fYm4o6Q1FEktFJ3PT3iBVElVt0oi0Cy4OMYrnmRy5tAFeDtJ5m3d3oJLMZ+JGMa+Ukexl/eRXPUYE1MA1AWtlY3/xm8ILtWK0lnebHCc6+G1py2A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713967563; c=relaxed/simple; bh=DD7I2+/OSFAuYPjbxmMle8pdnVsY53Td8fq7KEPM4QQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Nm9BIhvWu0YcIwOMmK6lyCWtgxOdHwyPjhvsMTLBCpmFkSuhZr8Bf4ZtTqKWJmS/OcA556f5OJ6STczCPApjSMt/TsHV0VninJnxTlYgl8Uu22NkmBcI1QtKZ+2voifRYDp7Y8/Y3SqQgsFQDlpd+IeeOyKhLeIOnU1HtKWZg8w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=hzED4HNd; arc=none smtp.client-ip=209.85.221.43 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="hzED4HNd" Received: by mail-wr1-f43.google.com with SMTP id ffacd0b85a97d-34a7e47d164so4369323f8f.3 for ; Wed, 24 Apr 2024 07:05:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1713967556; x=1714572356; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=LdQ0ElNNjcpbQIpJ1xf2rchBloiiwlw1canEoGrpfWE=; b=hzED4HNdRtQ43j1TOJn/SCUtGzmbQ2wcRpxFKKEZJL3fdluHrbE7s2St1n0mZYBprT NlBpq4Y8qreXUR9nvWqay2WXP+1h419lQG3MzyM2PWkX8tGayFD8bItl6y9zqYPaPsyq pb5Nmihjv7k92XZ5ClmzTbnoMfbeyryHK8MZw0c3vK0ro6IK6Mc0rZg6MZN0pS7+x/8p uPZpRVup4RL3S3ksYEsyGXqpNB2RRK9dfvAuFyadnKOK6L+aL2AF94PQngTISu770mHd gGAMiFNNOrevksHScuifuquvv2jiZnjKM6raC8axudgJFVZpXrh9yRcNo9vbidZykzSi r5ww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713967557; x=1714572357; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=LdQ0ElNNjcpbQIpJ1xf2rchBloiiwlw1canEoGrpfWE=; b=IyoTYI5jttWLJ/kwYE7WSbQ+KtkRNwYZkO1L8YtsXNmtULXAffZtlyXMlNKn0b6WvV kQRfR8aqZ3AC0mIemiTCuKnEW8Wbw4f5LNWnZM23a0Ks9KzEgzipIs2dGjtCFTKYAEI9 8epv4+mOp8wqNYHVHy6h07gSPFDxrbRIPr8MRqZvFPcViVSYRdzVLvij4nnZptlS2J0o fmLwINq0ZR8qS4QLd2eoIeEKAaCIt6ZyXMdwCOBb9+trw/gQzm7LADlAhxsaZQmnov2e ukMSRsv/TWXlfGTT68gwZvuoUQ2Vmk4Q7tbyy9zzqzJNmjCAef5uNMCvjQjLMIPw0sKb 2vEw== X-Forwarded-Encrypted: i=1; AJvYcCU4eJaBx3M0Mt3eeoFLkFdI8hWPXM2BuE/zqRQXyd4inny4vqWIZ3N1dLa2ccYPQyXAOfD8FBJB4wFeC5G7NCdZ9y6ShOx0Q4emW069 X-Gm-Message-State: AOJu0YyODHiYAkD5FR2376CVFiN75Uybj9q7rFFq4lSs1WZ9iRQoUN4T ZW1vVc+F4w2SwaXt7Y9uBtMkof3Jl9BbpDyIHYjtj3jTKKq6RLkpLjFxwkOJUf2Y/Q9iZQ348Tb yNyc= X-Google-Smtp-Source: AGHT+IEbhdGUtN0Z8RrMf55UUZ1158vNt0de2JPc+rWO8Iun7mc3/roCSNL98rGwj3hJonYnIMLxqA== X-Received: by 2002:adf:a3c1:0:b0:348:c2c7:efd3 with SMTP id m1-20020adfa3c1000000b00348c2c7efd3mr1635594wrb.17.1713967556721; Wed, 24 Apr 2024 07:05:56 -0700 (PDT) Received: from aspen.lan (aztw-34-b2-v4wan-166919-cust780.vm26.cable.virginm.net. [82.37.195.13]) by smtp.gmail.com with ESMTPSA id p8-20020a5d48c8000000b0034af40b2efdsm9105325wrs.108.2024.04.24.07.05.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Apr 2024 07:05:55 -0700 (PDT) From: Daniel Thompson Date: Wed, 24 Apr 2024 15:03:38 +0100 Subject: [PATCH v3 5/7] kdb: Use format-specifiers rather than memset() for padding in kdb_read() Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20240424-kgdb_read_refactor-v3-5-f236dbe9828d@linaro.org> References: <20240424-kgdb_read_refactor-v3-0-f236dbe9828d@linaro.org> In-Reply-To: <20240424-kgdb_read_refactor-v3-0-f236dbe9828d@linaro.org> To: Jason Wessel , Douglas Anderson Cc: kgdb-bugreport@lists.sourceforge.net, linux-kernel@vger.kernel.org, Daniel Thompson , stable@vger.kernel.org, Justin Stitt X-Mailer: b4 0.13.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=1454; i=daniel.thompson@linaro.org; h=from:subject:message-id; bh=DD7I2+/OSFAuYPjbxmMle8pdnVsY53Td8fq7KEPM4QQ=; b=owEBbQKS/ZANAwAKAXzjJV0594ihAcsmYgBmKRGUcB8O+iLmigr8T2IVW4HGG/L6tk+6pvEd0 go9Kb3yjbyJAjMEAAEKAB0WIQQvNUFTUPeVarpwrPB84yVdOfeIoQUCZikRlAAKCRB84yVdOfeI offTD/9pWj5IWkhuuYbxzyB5xUws8ru2eJBhGkCaw1r+yP62FDECOfAGQUSp7rvl7RoSS5pNTl3 95INKRg1m8mJP0tcU8Frwd+dGeIac+MbX/u0TlAlMyJNJ5P35RXFaXu1WjKEivlHJxH2AY/aL+o mQRdZAq7pr+wHaV1WipO3GlwGO2dcT/iql2hn5AOK4BG521yoFvVQ/xYkAVr/7QHTGRJt1ou8Pf PK6TxpAyXzjf4/315QqSCbtWDxb2TGnY9EKG/CerYTij7brpikOAl6VcZZY6PeC8rRTEdb4d6Pt ugny7/UdY3HjKVmXiG7QmEIh5m7b/yJXiJUHClRgNkvDXn8rS9xEFqvNmpBlNsNIOllT77q8/Ss PIF23Q90NV5bM9dMZf9aC4oMQyQI3Euv3N0Jeic4RQgF0uZ+v6MtckOK2P/LwRMmJwndgvXGOax VMfsX8ZNU6tUv9gLQPyBkRq7FZFjQs3jS2Mp1K3X04O5uOZXG5oxwDXfk1/demVrWz50RrLyNIC IinhwYB0zXjzoBEU+vFO2nHNev7MfK+hKsGWCZFGRNrhtCgpol3yNCA5QxE01fpHn/QmQ72U3Ki ZSpdyepm4L52ZAdmwgg0ju4ByKluyTh6PKBt1GIiS/nI0RoCwtDXgL7LIdoO6P2+rHdojpqCvk9 prgzSR8mfHkK+2w== X-Developer-Key: i=daniel.thompson@linaro.org; a=openpgp; fpr=E38BE19861669213F6E2661AA8A4E3BC5B7B28BE Currently when the current line should be removed from the display kdb_read() uses memset() to fill a temporary buffer with spaces. The problem is not that this could be trivially implemented using a format string rather than open coding it. The real problem is that it is possible, on systems with a long kdb_prompt_str, to write past the end of the tmpbuffer. Happily, as mentioned above, this can be trivially implemented using a format string. Make it so! Cc: stable@vger.kernel.org Reviewed-by: Douglas Anderson Tested-by: Justin Stitt Signed-off-by: Daniel Thompson --- kernel/debug/kdb/kdb_io.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/kernel/debug/kdb/kdb_io.c b/kernel/debug/kdb/kdb_io.c index a73779529803f..2aeaf9765b248 100644 --- a/kernel/debug/kdb/kdb_io.c +++ b/kernel/debug/kdb/kdb_io.c @@ -318,11 +318,9 @@ static char *kdb_read(char *buffer, size_t bufsize) break; case 14: /* Down */ case 16: /* Up */ - memset(tmpbuffer, ' ', - strlen(kdb_prompt_str) + (lastchar-buffer)); - *(tmpbuffer+strlen(kdb_prompt_str) + - (lastchar-buffer)) =3D '\0'; - kdb_printf("\r%s\r", tmpbuffer); + kdb_printf("\r%*c\r", + (int)(strlen(kdb_prompt_str) + (lastchar - buffer)), + ' '); *lastchar =3D (char)key; *(lastchar+1) =3D '\0'; return lastchar; --=20 2.43.0 From nobody Sat May 18 05:53:12 2024 Received: from mail-wm1-f45.google.com (mail-wm1-f45.google.com [209.85.128.45]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5B73415D5A9 for ; Wed, 24 Apr 2024 14:06:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713967565; cv=none; b=CR1TheS1dybEih6aBwh62ufm86Ss8QQSCpr1KeszlcjADkoLUOXFqmHm+L8cBIpAHsbGMa7czCszSRH50R70vrUI3LQUYiv5XYOGaBwDTZOM1RM3gfysNLY21AQBumR8oSBVMfAjxrOg2dPvcAGVT3XXEmZXfsqgn+rbZXMEM+s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713967565; c=relaxed/simple; bh=m1nIw6K/wxbB+0xqkP9QU662J3mgsOoAQo76wtMC2n8=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=iA0/ZXdtt8zfXvesSHFfTcJDH4YEvmiA/c/z+wvqGIivMdgAbwbqCCtDMN9L0QPUbH1zqA0zQxeu4KBetJULDGFj+cwaSiNCYKzZkia7p1XgnO7z93VhSjZp2AvDuPZ44S1FcZQcomwT6dgrnoM4Wt1w39cb/OfMvAYf3ImJy08= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=qHuAsj4R; arc=none smtp.client-ip=209.85.128.45 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="qHuAsj4R" Received: by mail-wm1-f45.google.com with SMTP id 5b1f17b1804b1-41aa45251eeso20106025e9.3 for ; Wed, 24 Apr 2024 07:06:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1713967558; x=1714572358; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=W7xv5Gr4R7/WZHUDWU1nCwM+2Vbz0wc+mfKaI2SQTzI=; b=qHuAsj4RAh4vAeSZuQ+fjWM3ommnqMZyWiJk6IOUG/WYMEL/FFCZbxIv/pZoWdVHAo rnxgNWH9gyVAtBFP4anpTsmiO2PAptpyYJa27EPYSyrFVK1CMt95cUOD5/NE75HrM76D u9s36zzKNaZYKdaAUGWvHdzhWpfPA4COkYt07tHfGXlXkrpt/DtSTaRW9Z1ogYrfoHwt t3Rg1Df99RmYA4RyhvPGEMHMhN+dXlgyUNUOchhVVchzIY53EkptOXoUsibKsmdJVlGy Zaa4wpymUhucJuDo/zpN1BIYOllaImaT9HeG6Glwm3LHeb2GGaVSSmv3YM+frV8e6iEv pUGA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713967558; x=1714572358; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=W7xv5Gr4R7/WZHUDWU1nCwM+2Vbz0wc+mfKaI2SQTzI=; b=cSbYWTbmMRmNuDoaWqE1BQtzIatq+Rg7qa672uvmICiVLows6QG2+TUG0VrsSyqyIx QnxDcaubLDun7Et92gcxofdW6JrbLVcMF4aefgQDCppszGuloPWQDCj2U01WLUbrzx5e lAnduCaOH/llBJ05smfuC01csmeuWzc+1F0Qks6gY4SlnG9/FVEWOzYaKOmU4MLlueOP m/cd6ut4dXWdTMKYHCm1VDCTmnhKFgzi/nWofAWoGpe3K0HKaJN6qZCyxlyCWVO7v0Zq h1tFeuKuB1zoogFtF5MXcnny0mS1rxU3U0hpmGgD4oWL/mLXpUF8PhxfHgheUGa8Beyi Gq3g== X-Forwarded-Encrypted: i=1; AJvYcCX5/AH0ryvUAMEbVrIuENPHLRwLTQbWRQgZGhkyWdeiE3jAiH0E4q8jDBeSKNPBB7Juu9M+Z7C+uuCTQdv9R8C7uUIVnx45LZEUOx+M X-Gm-Message-State: AOJu0YyKXSaV/q6e+kGpiPZy3ii+lXbUBTXA62zttmA5SK+2d2wrsZSI SDY7gShUHrQLB6/ssC7SKwjITOTD90IPh54BmxYe8VP3O9NyzSC13PCLNUglxQWQLhXZJLO0mO8 7ek0= X-Google-Smtp-Source: AGHT+IFq937L1Vi7JpHySQ2WjDRGjy5vgDcZRame4pNbvUA68UU9PQwXRC9KVlFkoL4mT3S8Fd8sgw== X-Received: by 2002:adf:e50b:0:b0:343:efb7:8748 with SMTP id j11-20020adfe50b000000b00343efb78748mr1632812wrm.66.1713967558452; Wed, 24 Apr 2024 07:05:58 -0700 (PDT) Received: from aspen.lan (aztw-34-b2-v4wan-166919-cust780.vm26.cable.virginm.net. [82.37.195.13]) by smtp.gmail.com with ESMTPSA id p8-20020a5d48c8000000b0034af40b2efdsm9105325wrs.108.2024.04.24.07.05.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Apr 2024 07:05:57 -0700 (PDT) From: Daniel Thompson Date: Wed, 24 Apr 2024 15:03:39 +0100 Subject: [PATCH v3 6/7] kdb: Replace double memcpy() with memmove() in kdb_read() Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20240424-kgdb_read_refactor-v3-6-f236dbe9828d@linaro.org> References: <20240424-kgdb_read_refactor-v3-0-f236dbe9828d@linaro.org> In-Reply-To: <20240424-kgdb_read_refactor-v3-0-f236dbe9828d@linaro.org> To: Jason Wessel , Douglas Anderson Cc: kgdb-bugreport@lists.sourceforge.net, linux-kernel@vger.kernel.org, Daniel Thompson , Justin Stitt X-Mailer: b4 0.13.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2464; i=daniel.thompson@linaro.org; h=from:subject:message-id; bh=m1nIw6K/wxbB+0xqkP9QU662J3mgsOoAQo76wtMC2n8=; b=owEBbQKS/ZANAwAKAXzjJV0594ihAcsmYgBmKRGhHh0sDmQu0SuR6ilg48GgyUTWyWMpc6f/b 7cMSMspygCJAjMEAAEKAB0WIQQvNUFTUPeVarpwrPB84yVdOfeIoQUCZikRoQAKCRB84yVdOfeI oWitD/92rzCg+qesbfCyE2mJoqhkiy0mr+6LY0ERzA4UQyQczanvfLm/oDA7/lgsVhNoqcc1mQr WJXP0XD7ZcigdkkOCQxM6XtTqotz6YvjxpLxcVVFgx1A4Of79ca6WkZGtC2F8QN5/kGzcpZqwjx hrBGYW8zG/5UGFz+91CQ+acTceX/bmbDpJq4FDVRSIdX0BAFzWFmliRA1ayfmOl95kWrd9COgSY TakLYAogsd5nOMUL3d2mFcQWK7ZqHl1+tv9NA67ZobfJ2QIjmOfOe+vTBNXE8UiGlTnR/XqhQQI B51sjhxd/MzAYlBPuM1odRN6eYmtH4WWLTmn0EKf/M+cpTXSOpTtqRt9JkpWkT/hd0KpPkJvchq ASsZMIo97PJY28gmN91fE0JDT/VtxwqHGp4tM84+DUZK9w3IlJOEDW28c1OuIx9s6BCMYrCSVPM ERusAc8+EtWW9vw95wYHRZOtjgeDklxQb+Zk+G8/Do8JYRr+82vnEl8x0VSkeqgX35azMroX3Gx YrCjphlt3LxHzcUZbKnEbHVNzmaQs8dcsYkGHthMCpSvql1pgbRW9zCWP0ONSMQ4L2ojQL/pzbR WKbdl+oPfBbYEyOBEMOX3bBEgcD/1il/y1JcUsZ5tqGR8dMhotfw4yBdsd/0qKvb+SbeCEPGcQW r3MFtMTucJ/0+oQ== X-Developer-Key: i=daniel.thompson@linaro.org; a=openpgp; fpr=E38BE19861669213F6E2661AA8A4E3BC5B7B28BE At several points in kdb_read() there are variants of the following code pattern (with offsets slightly altered): memcpy(tmpbuffer, cp, lastchar - cp); memcpy(cp-1, tmpbuffer, lastchar - cp); *(--lastchar) =3D '\0'; There is no need to use tmpbuffer here, since we can use memmove() instead so refactor in the obvious way. Additionally the strings that are being copied are already properly terminated so let's also change the code so that the library calls also move the terminator. Changing how the terminators are managed has no functional effect for now but might allow us to retire lastchar at a later point. lastchar, although stored as a pointer, is functionally equivalent to caching strlen(buffer). Reviewed-by: Douglas Anderson Tested-by: Justin Stitt Signed-off-by: Daniel Thompson --- kernel/debug/kdb/kdb_io.c | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/kernel/debug/kdb/kdb_io.c b/kernel/debug/kdb/kdb_io.c index 2aeaf9765b248..40617f36a6db4 100644 --- a/kernel/debug/kdb/kdb_io.c +++ b/kernel/debug/kdb/kdb_io.c @@ -269,12 +269,9 @@ static char *kdb_read(char *buffer, size_t bufsize) switch (key) { case 8: /* backspace */ if (cp > buffer) { - if (cp < lastchar) { - memcpy(tmpbuffer, cp, lastchar - cp); - memcpy(cp-1, tmpbuffer, lastchar - cp); - } - *(--lastchar) =3D '\0'; - --cp; + memmove(cp-1, cp, lastchar - cp + 1); + lastchar--; + cp--; kdb_printf("\b%s ", cp); kdb_position_cursor(kdb_prompt_str, buffer, cp); } @@ -291,9 +288,8 @@ static char *kdb_read(char *buffer, size_t bufsize) return buffer; case 4: /* Del */ if (cp < lastchar) { - memcpy(tmpbuffer, cp+1, lastchar - cp - 1); - memcpy(cp, tmpbuffer, lastchar - cp - 1); - *(--lastchar) =3D '\0'; + memmove(cp, cp+1, lastchar - cp); + lastchar--; kdb_printf("%s ", cp); kdb_position_cursor(kdb_prompt_str, buffer, cp); } @@ -398,9 +394,8 @@ static char *kdb_read(char *buffer, size_t bufsize) default: if (key >=3D 32 && lastchar < bufend) { if (cp < lastchar) { - memcpy(tmpbuffer, cp, lastchar - cp); - memcpy(cp+1, tmpbuffer, lastchar - cp); - *++lastchar =3D '\0'; + memmove(cp+1, cp, lastchar - cp + 1); + lastchar++; *cp =3D key; kdb_printf("%s", cp); ++cp; --=20 2.43.0 From nobody Sat May 18 05:53:12 2024 Received: from mail-wm1-f52.google.com (mail-wm1-f52.google.com [209.85.128.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8CB5715ECFB for ; Wed, 24 Apr 2024 14:06:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713967564; cv=none; b=cFZufJSJ3mCDjI3CKeL91WdRFcmw1OQ6a0OisG67vObiJJrpgP8dUa17nbqagdNIn5TwYhVtnhd05MduRfe3ugtyKKindmUa1Q3SlLtgCWFCp28uDst8Jlie4XwGRUZhSDGcQNfyD4lDzvDqOGnsAgOGEfUwTr+yF46G1PTh1HM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713967564; c=relaxed/simple; bh=4y/UfQeo9BB5p962BWa2HGfLqeDinyewUKR63C4ycb0=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=acUs/8ck/0bXabEeNMtyTwSe6zbGX7zM540jqKFnice6pHdpU5ipvY6EYlYmyocQJ9l74h8zzFd3K/fVdL2UhUdn2hcSoD5WRWrcnin+NhC7wcGlIoOvF6jOfyIFqF18krQCfunLMuGCoGkSRHeLAEsi5+dVxc0bfj8sUvtBKaw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=ZaabQzwt; arc=none smtp.client-ip=209.85.128.52 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="ZaabQzwt" Received: by mail-wm1-f52.google.com with SMTP id 5b1f17b1804b1-41aa21b06b3so27274435e9.0 for ; Wed, 24 Apr 2024 07:06:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1713967559; x=1714572359; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=xf6jJgGtYoURMWCLwezA7KydSJS05u43a8wuFVX3hek=; b=ZaabQzwtshVQhlaHpD1qSz5Rs3rcSlgfeviWAADug8QqpFq2hCK2t3gUPFkpPMgMYL 0CALT5OXAdVkP9VuM+oQUMnh5thbwo1Rn/9+J+rQZaUbyTEGyRLU/73nhSLpOGlea7Zn Pqp6jt+FD+I05LvU7ophQMvD6emoX8slmInQ+LYt5hs3vc8SqatUJLSYcprpz1w81cRE 0b+okow2Ix2nD2aBICdlypxIjSIF9J9CKHCrpZrso7Ss/QmLEN1BGGUPipY9K7FQmJOv 2bbm+EM+Q21JtevzuosHDQJxD5V1W0QEpPeLa/aMitCaMLufbNdUp2zUA0NfGwUER0M+ +dZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713967559; x=1714572359; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=xf6jJgGtYoURMWCLwezA7KydSJS05u43a8wuFVX3hek=; b=Eo3lxUv/I0LJQryLdZfvyToWEzh+YXdSsxIQGKFUCJoUlLkP2sMfTZsEczn+HwkVAy 3GnfnQDONh8Db5RIZI+oiE/hfFpHi93x45lFI4ZxS9yejZGi1IfuFaCqED6ifNXq+meQ xrMmaX25ZSIlCgIrAGvEkMcy8EMQzNp61NmNe8TP/eYoqm9UWNFUCUjQ8LjbXfVWaZJq MHWgomoK3LRYMK0CjDD+Gv9NQn/M/Hv+5kjeUZfuRCI5zq0j3JR1yrRs8d5Dg+uT/VG0 7Z/P27dzAvUqXp/cmgqwWkFdP0MpYaaeBY94+2voJAjqkjz/tBM/lp572gAsRZAHtxcZ iMig== X-Forwarded-Encrypted: i=1; AJvYcCWU8SGpwR5dDPLd61CCIDyTnj+AUmNDtF+npnb57arJ5ALLu2fBz9fOA5SXJheurWEg52CwlyFgpLmjmu/EHrYMUdecsQeHIkwq5TuI X-Gm-Message-State: AOJu0YzBf2F6gvvsAAQafHDm9WQZvFI/uLhcvdLbrJpST3rvXhictpD5 x/OQ94AWUD7WTvdB6nhwsLIKy4gtWmBmm0u2v5foTt1XS3HuElnLnPkRIUXu+t0dsGgUXR4DgoH QwGs= X-Google-Smtp-Source: AGHT+IGd68kgcf+HjE1TmNyFnjhhLSnTd0wJKFJJOgOaYKRHvfSqLflyrYYA/pJTiUGWJpppqsdsMQ== X-Received: by 2002:adf:f1c4:0:b0:34a:5663:40b with SMTP id z4-20020adff1c4000000b0034a5663040bmr2105456wro.3.1713967559219; Wed, 24 Apr 2024 07:05:59 -0700 (PDT) Received: from aspen.lan (aztw-34-b2-v4wan-166919-cust780.vm26.cable.virginm.net. [82.37.195.13]) by smtp.gmail.com with ESMTPSA id p8-20020a5d48c8000000b0034af40b2efdsm9105325wrs.108.2024.04.24.07.05.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Apr 2024 07:05:58 -0700 (PDT) From: Daniel Thompson Date: Wed, 24 Apr 2024 15:03:40 +0100 Subject: [PATCH v3 7/7] kdb: Simplify management of tmpbuffer in kdb_read() Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20240424-kgdb_read_refactor-v3-7-f236dbe9828d@linaro.org> References: <20240424-kgdb_read_refactor-v3-0-f236dbe9828d@linaro.org> In-Reply-To: <20240424-kgdb_read_refactor-v3-0-f236dbe9828d@linaro.org> To: Jason Wessel , Douglas Anderson Cc: kgdb-bugreport@lists.sourceforge.net, linux-kernel@vger.kernel.org, Daniel Thompson , Justin Stitt X-Mailer: b4 0.13.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=3701; i=daniel.thompson@linaro.org; h=from:subject:message-id; bh=4y/UfQeo9BB5p962BWa2HGfLqeDinyewUKR63C4ycb0=; b=owEBbQKS/ZANAwAKAXzjJV0594ihAcsmYgBmKRGudf5inuoD8Nr656uwiWRZbIEHdLW4JrDeZ lJMHhMk9KiJAjMEAAEKAB0WIQQvNUFTUPeVarpwrPB84yVdOfeIoQUCZikRrgAKCRB84yVdOfeI occbD/4pjpqPwrvEP/0VZBZncv0hccI6KG89x6KIdf9GTGF2RcxcXf908MP/MhPdHqHFBCKrIGY bwVl552feyjt0cvHDl0S56uvymM/PvQxvJ3Qx8Ab1xhRjlcd1VaFc3pCcJMEBFV9sLu1mr3N0wA aR+TyTxX3/BmHeHaveZEGP6aRDBQMn3W7tXBBleJzEA1L/8paW1XXFHgmtDSJ47IwQVcKZc4PsM P/B/8sISlay8zMAquHSal2ZwU0wOwMCfxv4qBsBhw2Alxvu/e+gAS529cnmSB32PLwbaysgB+rm v1Ydibd2VHGovJbY/waI07V1iakoEW5rirxS8CQfqR1jBlktMhovMvBvx/+fpEeMmjwlbxFFo19 cPe/AALhutiwT+aNLLlqevg40bpOh0ZH7GsNJLAxiiO+UyH3nQzQS0UQgEEJZFu0kmVuxogQZg2 17nnnbk1rs668oOA3XIoKzJU1Uc5EQTVu1WaU+snMQoswNX9P11elp0QaP+/8qNBE3Q2w17ucmU Eyqd66QjcvOnSIjunait0JmRcYstZD4ue+WcwJT54S6xdFDGWfT8K53szVurX20Zzmv4kO3ngEi fbe/ENy4aF9CQZ9YSZOdkXuHBeUaF81uJTxhwnkX5dMZqa+kK0V6AMNAWhPMFSwzGJUF5Yfo2GC /DiMt47Qrii1YYg== X-Developer-Key: i=daniel.thompson@linaro.org; a=openpgp; fpr=E38BE19861669213F6E2661AA8A4E3BC5B7B28BE The current approach to filling tmpbuffer with completion candidates is confusing, with the buffer management being especially hard to reason about. That's because it doesn't copy the completion canidate into tmpbuffer, instead of copies a whole bunch of other nonsense and then runs the completion search from the middle of tmpbuffer! Change this to copy nothing but the completion candidate into tmpbuffer. Pretty much everything else in this patch is renaming to reflect the above change: s/p_tmp/tmpbuffer/ s/buf_size/sizeof(tmpbuffer)/ Reviewed-by: Douglas Anderson Tested-by: Justin Stitt Signed-off-by: Daniel Thompson --- kernel/debug/kdb/kdb_io.c | 41 ++++++++++++++++++----------------------- 1 file changed, 18 insertions(+), 23 deletions(-) diff --git a/kernel/debug/kdb/kdb_io.c b/kernel/debug/kdb/kdb_io.c index 40617f36a6db4..3131334d7a81c 100644 --- a/kernel/debug/kdb/kdb_io.c +++ b/kernel/debug/kdb/kdb_io.c @@ -239,6 +239,7 @@ static char *kdb_read(char *buffer, size_t bufsize) * and null byte */ char *lastchar; char *p_tmp; + char tmp; static char tmpbuffer[CMD_BUFLEN]; int len =3D strlen(buffer); int len_tmp; @@ -246,8 +247,7 @@ static char *kdb_read(char *buffer, size_t bufsize) int count; int i; int diag, dtab_count; - int key, buf_size, ret; - + int key, ret; =20 diag =3D kdbgetintenv("DTABCOUNT", &dtab_count); if (diag) @@ -329,21 +329,16 @@ static char *kdb_read(char *buffer, size_t bufsize) case 9: /* Tab */ if (tab < 2) ++tab; - p_tmp =3D buffer; - while (*p_tmp =3D=3D ' ') - p_tmp++; - if (p_tmp > cp) - break; - memcpy(tmpbuffer, p_tmp, cp-p_tmp); - *(tmpbuffer + (cp-p_tmp)) =3D '\0'; - p_tmp =3D strrchr(tmpbuffer, ' '); - if (p_tmp) - ++p_tmp; - else - p_tmp =3D tmpbuffer; - len =3D strlen(p_tmp); - buf_size =3D sizeof(tmpbuffer) - (p_tmp - tmpbuffer); - count =3D kallsyms_symbol_complete(p_tmp, buf_size); + + tmp =3D *cp; + *cp =3D '\0'; + p_tmp =3D strrchr(buffer, ' '); + p_tmp =3D (p_tmp ? p_tmp + 1 : buffer); + strscpy(tmpbuffer, p_tmp, sizeof(tmpbuffer)); + *cp =3D tmp; + + len =3D strlen(tmpbuffer); + count =3D kallsyms_symbol_complete(tmpbuffer, sizeof(tmpbuffer)); if (tab =3D=3D 2 && count > 0) { kdb_printf("\n%d symbols are found.", count); if (count > dtab_count) { @@ -355,14 +350,14 @@ static char *kdb_read(char *buffer, size_t bufsize) } kdb_printf("\n"); for (i =3D 0; i < count; i++) { - ret =3D kallsyms_symbol_next(p_tmp, i, buf_size); + ret =3D kallsyms_symbol_next(tmpbuffer, i, sizeof(tmpbuffer)); if (WARN_ON(!ret)) break; if (ret !=3D -E2BIG) - kdb_printf("%s ", p_tmp); + kdb_printf("%s ", tmpbuffer); else - kdb_printf("%s... ", p_tmp); - *(p_tmp + len) =3D '\0'; + kdb_printf("%s... ", tmpbuffer); + tmpbuffer[len] =3D '\0'; } if (i >=3D dtab_count) kdb_printf("..."); @@ -373,14 +368,14 @@ static char *kdb_read(char *buffer, size_t bufsize) kdb_position_cursor(kdb_prompt_str, buffer, cp); } else if (tab !=3D 2 && count > 0) { /* How many new characters do we want from tmpbuffer? */ - len_tmp =3D strlen(p_tmp) - len; + len_tmp =3D strlen(tmpbuffer) - len; if (lastchar + len_tmp >=3D bufend) len_tmp =3D bufend - lastchar; =20 if (len_tmp) { /* + 1 ensures the '\0' is memmove'd */ memmove(cp+len_tmp, cp, (lastchar-cp) + 1); - memcpy(cp, p_tmp+len, len_tmp); + memcpy(cp, tmpbuffer+len, len_tmp); kdb_printf("%s", cp); cp +=3D len_tmp; lastchar +=3D len_tmp; --=20 2.43.0