Skip to content

Word8VectorSlice: mapping a subslice produces wrong result or crashes SML/NJ #63

@JohnReppy

Description

@JohnReppy

Version

110.99.2

Operating System

  • All
  • Linux
  • macOS
  • Windows
  • Other Unix

OS Version

No response

Processor

Any

Component

Basis Library

Severity

Major

Description of the problem

There are some inputs for which using Word8VectorSlice.map on the result of
Word8VectorSlice.subslice would either produce an incorrect result, or would
cause SML/NJ to crash with "Fatal error -- bad object tag 28".

I have attached source code that creates a Word8VectorSlice, then creates a
subslice of it using Word8VectorSlice.subslice, then maps the subslice using
Word8VectorSlice.map, followed by printing the results of the map operation.

Run the code using sml < the-program.sml. The "Fatal error" only occurs
occasionally. You may observe the fatal error by running the program repeatedly
using this shell snippet:

while :; do sml < the-program.sml; [ $? -ne 0 ] && break; done

Transcript

This is the result that SML/NJ may produce:

    father length: 6
    CHAR 0: f
    CHAR 1: a
    CHAR 2: t
    CHAR 3: h
    CHAR 4: e
    CHAR 5: r
    mappedFather length: 6
    CHAR 0: \^@
    CHAR 1: \^@
    CHAR 2: \^@
    CHAR 3: f
    CHAR 4: a
    CHAR 5: t


This is supposed to be the correct result:

    father length: 6
    CHAR 0: f
    CHAR 1: a
    CHAR 2: t
    CHAR 3: h
    CHAR 4: e
    CHAR 5: r
    mappedFather length: 6
    CHAR 0: f
    CHAR 1: a
    CHAR 2: t
    CHAR 3: h
    CHAR 4: e
    CHAR 5: r

Expected Behavior

No response

Steps to Reproduce

(* This is supposed to behave like an identity function for Word8VectorSlice.
 * Word8VectorSlice.slice -> Word8VectorSlice.slice *)
fun uselessMap slice =
  Word8VectorSlice.full (Word8VectorSlice.map (fn word => word) slice)

fun printWord8VectorSlice slice =
  Word8VectorSlice.appi
    (fn (index, word) => (
      print ("CHAR " ^ Int.toString index ^ ": ");
      print (Char.toString (Char.chr (Word8.toInt word)));
      print "\n"))
    slice

fun charsToWord8List charList =
  List.map (fn c => Word8.fromInt (Char.ord c)) charList

fun main () =
  let
    val vec = Word8Vector.fromList (
                charsToWord8List [#"m", #"y", #" ", #"f", #"a", #"t", #"h", #"e", #"r"])
    val slice = Word8VectorSlice.full vec  (* Word8Vector.vector -> Word8VectorSlice.slice *)
    val father = Word8VectorSlice.subslice (slice, 3, NONE)  (* Exclude "my " *)
    val mappedFather = uselessMap father
  in
    print ("father length: " ^ Int.toString (Word8VectorSlice.length father) ^ "\n");
    printWord8VectorSlice father;
    (* Mapped father *)
    print ("mappedFather length: " ^ Int.toString (Word8VectorSlice.length mappedFather) ^ "\n");
    printWord8VectorSlice mappedFather;
    ()
  end

val () = main ()

Additional Information

No response

Email address

No response

Comments from smlnj-gforge

Original smlnj-gforge bug number 306

Submitted on 2022-05-10 at 10:59:00

comment by @JohnReppy on 2022-05-10 15:39:00 +000 UTC

Stupid indexing bug in Word8Slice.map code. Does not appear to affect any of the other vector-slice modules. Fixed for 110.99.3 and 2022.1.

Metadata

Metadata

Assignees

Labels

basis-libproblem with Standard ML Basis librarybugSomething isn't workingfixed-in-110.99.3issues that will be fixed in the 110.99.3 versiongforgebug (or feature request) ported from smlnj-gforge repository

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions