Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modifying a seq after unmarshling sometimes triggers am Attempt to read from nil? #12405

Closed
pedromsilvapt opened this issue Oct 10, 2019 · 0 comments · Fixed by #24119
Closed

Comments

@pedromsilvapt
Copy link

Modifying a seq after unmarshling sometimes triggers am Attempt to read from nil?. If I read the sequence without modifying no error happens.

Example

import marshal
import streams
import times
import tables

type AiredEpisodeState * = ref object
    airedAt * : DateTime
    tvShowId * : string
    seasonNumber * : int
    number * : int
    title * : string

type ShowsWatchlistState * = ref object
    aired * : seq[AiredEpisodeState]

type UiState * = ref object
    shows: ShowsWatchlistState

# Helpers to marshal and unmarshal
proc load * ( state : var UiState, file : string ) =
    var strm = newFileStream( file, fmRead )

    strm.load( state )

    strm.close()

proc store * ( state : UiState, file : string ) =
    var strm = newFileStream( file, fmWrite )

    strm.store( state )

    strm.close()

# 1. We fill the state initially
var state : UiState = UiState( shows: ShowsWatchlistState( aired: @[] ) )

# VERY IMPORTANT: For some reason, small numbers (like 2 or 3) don't trigger the bug. Anything above 7 or 8 on my machine triggers though
for i in 0..30:
    var episode = AiredEpisodeState( airedAt: now(), tvShowId: "1", seasonNumber: 1, number: 1, title: "string" )

    state.shows.aired.add( episode )

# 2. Store it in a file with the marshal module, and then load it back up
store( state, "repro.dat" )
load( state, "repro.dat" )

# 3. VERY IMPORTANT: Without this line, for some reason, everything works fine
state.shows.aired[ 0 ] = AiredEpisodeState( airedAt: now(), tvShowId: "1", seasonNumber: 1, number: 1, title: "string" )

# 4. And formatting the airedAt date will now trigger the exception
for ep in state.shows.aired:
    echo( $ep.seasonNumber & "x" & $ep.number & " (" & $ep.airedAt & ")" )

Current Output

C:\Users\PedroSilva\Documents\Projects\Nim\watchlist\src\repro.nim(52) repro
C:\Program Files\nim-1.0.0\lib\pure\times.nim(2427) $
C:\Program Files\nim-1.0.0\lib\pure\times.nim(2308) format
C:\Program Files\nim-1.0.0\lib\pure\times.nim(2290) format
C:\Program Files\nim-1.0.0\lib\pure\times.nim(2005) formatPattern
C:\Program Files\nim-1.0.0\lib\system\strmantle.nim(24) eqStrings
SIGSEGV: Illegal storage access. (Attempt to read from nil?)

Expected Output

1x1 (2019-10-10T12:26:01+01:00)
1x1 (2019-10-10T12:26:01+01:00)
1x1 (2019-10-10T12:26:01+01:00)
...

Possible Solution

Additional Information

$ nim -v
Nim Compiler Version 1.0.0 [Windows: amd64]
Compiled at 2019-09-23
Copyright (c) 2006-2019 by Andreas Rumpf

git hash: f7a8fc46c0012033917582eb740dc0343c093e35
active boot switches: -d:release
Araq pushed a commit that referenced this issue Aug 30, 2024
closes #1969, closes #7547, closes #7737, closes #11838, closes #12283,
closes #12714, closes #12720, closes #14053, closes #16118, closes
#19670, closes #22645

I was going to wait on these but regression tests even for recent PRs
are turning out to be important in wide reaching PRs like #24010.

The other issues with the working label felt either finnicky (#7385,
#9156, #12732, #15247), excessive to test (#12405, #12424, #17527), or I
just don't know what fixed them/what the issue was (#16128: the PR link
gives a server error by Github, #12457, #12487).
metagn added a commit to metagn/Nim that referenced this issue Sep 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants