[hackers] [sbase] dc: Relax tail call optimization || Roberto E. Vargas Caballero

From: <git_AT_suckless.org>
Date: Sat, 17 Jan 2026 11:01:22 +0100 (CET)

commit 2544b70216bb71aec663a9657142e13ad0c6ac41
Author: Roberto E. Vargas Caballero <k0ga_AT_shike2.net>
AuthorDate: Sat Jan 17 10:57:43 2026 +0100
Commit: Roberto E. Vargas Caballero <k0ga_AT_shike2.net>
CommitDate: Sat Jan 17 10:57:43 2026 +0100

    dc: Relax tail call optimization
    
    Classical dc implementations only apply tail recursion optimization
    but we were applying tail call recursion, removing one frame even
    when no recursion was involved. This creates problems with bc that
    does not track this optimization and it generates values for the Q
    command without caring about this optimization.

diff --git a/dc.c b/dc.c
index 092afc0..8017c58 100644
--- a/dc.c
+++ b/dc.c
_AT_@ -1892,14 +1892,14 @@ execmacro(void)
                 return;
         }
 
- /* check for tail recursion */
- for (ch = *input->s; ch > 0 && ch < UCHAR_MAX; ch = *input->s) {
+ for (ch = *input->s; ch > 0 && ch <= UCHAR_MAX; ch = *input->s) {
                 if (!isspace(ch))
                         break;
                 ++input->s;
         }
 
- if (ch == '\0') {
+ /* check for tail recursion */
+ if (ch == '\0' && strcmp(input->buf, v.u.s) == 0) {
                 free(input->buf);
                 input->buf = input->s = v.u.s;
                 return;
diff --git a/tests/0045-dc.sh b/tests/0045-dc.sh
new file mode 100755
index 0000000..a4a22e1
--- /dev/null
+++ b/tests/0045-dc.sh
_AT_@ -0,0 +1,19 @@
+#!/bin/sh
+
+set -e
+
+tmp=$$.tmp
+
+trap 'rm -f $tmp' EXIT
+trap 'exit $?' HUP INT TERM
+
+echo 0 > $tmp
+
+../dc -i <<'EOF' | diff -u - $tmp
+[ 0 Lxs. 2Q]s<128>
+[ .7853981633974483096156608458198757210492923498437764 1/ Lxs. 3Q]s<130>
+[K 52><130> ]s<129>
+[Sxlx 0=<128> lx 1=<129> 0 Lxs. 1Q]s<1>
+
+ 1l<1>xps.
+EOF
Received on Sat Jan 17 2026 - 11:01:22 CET

This archive was generated by hypermail 2.3.0 : Sat Jan 17 2026 - 11:12:31 CET