Skip to content

Commit

Permalink
testament: support \0 byte in output comparison (#1083)
Browse files Browse the repository at this point in the history
## Summary

Neither the program output nor the comparison string are cut off at the
first null byte anymore.

## Details

Output comparisons in testament used `strutils.contains`, which uses
`strutils.find`, which, by default, uses `c_strstr`.

`c_strstr` operates on null-terminated strings, and thus stopped at the
first occurrence of one, even if the end of the NimSkull string hasn't
been reached yet.

Testament now uses a custom `contains` implementation based on C's
`memcmp`, so that the `\0` byte doesn't terminate the search.
  • Loading branch information
zerbina authored Jan 9, 2024
1 parent d7e28ea commit bcedd54
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
25 changes: 24 additions & 1 deletion testament/testament.nim
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@
## Testament runs tests for the compiler.

import std/[
strutils, pegs, os, osproc, streams, json, parseopt, browsers,
pegs, os, osproc, streams, json, parseopt, browsers,
terminal, algorithm, times, md5, intsets, macros, tables,
options, sequtils, hashes
]
import system/platforms
import backend, htmlgen, specs
# the ``contains`` overload for strings from strutils doesn't support NULL
# chars...
import std/strutils except contains
from std/sugar import dup
import compiler/utils/nodejs
import lib/stdtest/testutils
Expand Down Expand Up @@ -190,6 +193,26 @@ let

# ----------------------------------------------------------------------------

proc memcmp(a, b: pointer, len: csize_t): cint {.importc, header: "<string.h>".}

{.push checks: off.} # for efficiency, omit all run-time checks

proc contains(a, b: string): bool =
## Returns whether `b` is part of `a`, but doesn't cut off strings at the first
## NULL byte. If `b` is empty, 'true' is returned.
if b.len == 0:
return true

let c = b[0]
# search for `b`'s first character in `a`, then do a memcmp
for i in 0 .. (a.len - b.len):
if a[i] == c and memcmp(addr a[i], addr b[0], csize_t(b.len)) == 0:
return true

result = false

{.pop.}

proc trimUnitSep(x: var string) =
let L = x.len
if L > 0 and x[^1] == '\31':
Expand Down
6 changes: 6 additions & 0 deletions testament/tests/shouldfail/toutput_null_byte.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
discard """
output: "a\0b"
"""

# the string differ after the NULL byte
echo "a\0c"
2 changes: 2 additions & 0 deletions tests/testament/tshould_not_work.nim
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ FAIL: tests/shouldfail/tnimoutfull.nim
Failure: reMsgsDiffer
FAIL: tests/shouldfail/toutput.nim
Failure: reOutputsDiffer
FAIL: tests/shouldfail/toutput_null_byte.nim
Failure: reOutputsDiffer
FAIL: tests/shouldfail/toutputsub.nim
Failure: reOutputsDiffer
FAIL: tests/shouldfail/treject.nim
Expand Down

0 comments on commit bcedd54

Please sign in to comment.