Subject: bug#63196: sxml or guile bug?

From: Christopher Lam <[email-masked]>
Date: Mon, 1 May 2023 10:32:26 +0800

Hi, we've identified an unusual behaviour in writing a test in gnucash. Can
anyone reproduce and confirmit?

I paste a simple .scm file, and its input test-file.html. Now, in
sxml-bug.scm, modify the <tr> index - from -1 to -2 i.e. access the
last/penultimate row etc.

Odd negative indices work, even negative indices lead to error. Odd (haha)
bug. See report for (tr -2) for example, pasted below.

==== sxml-bug.scm =====
(use-modules (sxml simple))
(use-modules (sxml xpath))
(use-modules (ice-9 textual-ports))
(define html (call-with-input-file "/tmp/test-file.html" get-string-all))
(define sx (xml->sxml html))

;; note this path: try different indices for the tr tag:
;; odd numbers -1 -3 -5 are ok
;; even numbers -2 -4 -6 will fail
(define path '(// (table 1) // (tr -1)))

(format #t "html=[~s]\n" html)
(format #t "sxpath is [~s]\n" path)
(format #t "result is [~s]\n" ((sxpath path) sx))
====

And the backtrace with -2

=====
Backtrace:
In ice-9/boot-9.scm:
  1752:10 18 (with-exception-handler _ _ #:unwind? _ # _)
In unknown file:
          17 (apply-smob/0 #<thunk 7f7fd530d2e0>)
In ice-9/boot-9.scm:
    724:2 16 (call-with-prompt _ _ #<procedure default-prompt-handle…>)
In ice-9/eval.scm:
    619:8 15 (_ #(#(#<directory (guile-user) 7f7fd5312c80>)))
In ice-9/boot-9.scm:
   2836:4 14 (save-module-excursion _)
  4388:12 13 (_)
In /home/user/sources/sxml-bug.scm:
    20:30 12 (_)
In sxml/xpath.scm:
   254:18 11 (map-union #<procedure 7f7fd18e8280 at sxml/xpath.scm:…> …)
   254:18 10 (map-union #<procedure 7f7fd18e8280 at sxml/xpath.scm:…> …)
   254:18  9 (map-union #<procedure 7f7fd18e8280 at sxml/xpath.scm:…> …)
   254:18  8 (map-union #<procedure 7f7fd18e8280 at sxml/xpath.scm:…> …)
   254:18  7 (map-union #<procedure 7f7fd18e8280 at sxml/xpath.scm:…> …)
   252:22  6 (map-union #<procedure 7f7fd18e8280 at sxml/xpath.scm:…> …)
   252:22  5 (map-union #<procedure 7f7fd18f2ea0 at sxml/xpath.scm:…> …)
   254:18  4 (map-union #<procedure 7f7fd1901420 at sxml/xpath.scm:…> …)
   254:18  3 (map-union #<procedure 7f7fd1901420 at sxml/xpath.scm:…> …)
   252:22  2 (map-union #<procedure 7f7fd1901420 at sxml/xpath.scm:…> …)
   484:19  1 (loop _ ((tr -6)))
   181:24  0 (_ _)

sxml/xpath.scm:181:24: yikes!
=====