Subject: bug#50764: Fix (string-distance "" "") edge-case

From: Philip Kaludercic <[email-masked]>
Date: Thu, 23 Sep 2021 16:16:42 +0000

Tags: patch

string-distance appears to be using uninitialized memory, if the second
argument is an empty string. This leads to the function occasionally
generating random and meaningless results. This patch ensures that the
entire memory is initialized before it is accessed, fixing the results
for edge cases with empty strings.

In GNU Emacs 28.0.50 (build 13, x86_64-pc-linux-gnu, X toolkit, cairo version 1.16.0, Xaw scroll bars)
 of 2021-09-23 built on icterid
Repository revision: bb5d8582f75712c3610795aa5ead79600308f8bb
Repository branch: master
Windowing system distributor 'The X.Org Foundation', version 11.0.12011000
System Description: Debian GNU/Linux 11 (bullseye)

Configured using:
 'configure LDFLAGS=-flto 'CFLAGS=-O2 -march=native -mtune=native -pipe'
 --with-native-compilation PKG_CONFIG_PATH='

>From fbc62cb035fe2a674c9834c6bdcef5c5d2af85e2 Mon Sep 17 00:00:00 2001
From: Philip Kaludercic <[email-masked]>
Date: Thu, 23 Sep 2021 18:12:41 +0200
Subject: [PATCH] Fix string-distance for two empty strings

* fns.c (Fstring_distance): Avoid using uninitialized memory
---
 src/fns.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/fns.c b/src/fns.c
index 4e74589ef2..a72e41aee5 100644
--- a/src/fns.c
+++ b/src/fns.c
@@ -322,7 +322,7 @@ DEFUN ("string-distance", Fstring_distance, Sstring_distance, 2, 3, 0,
 
   USE_SAFE_ALLOCA;
   ptrdiff_t *column = SAFE_ALLOCA ((len1 + 1) * sizeof (ptrdiff_t));
-  for (y = 1; y <= len1; y++)
+  for (y = 0; y <= len1; y++)
     column[y] = y;
 
   if (use_byte_compare)
-- 
2.30.2

-- 
	Philip K.