Infinitely recursive values stack-overflow with the --json output format #772

Open
opened 2025-03-26 18:34:13 +00:00 by jade · 2 comments
Owner

Describe the bug

$ nix eval --expr 'let self = { inherit self; foo = 2; }; in self'
{ foo = 2; self = «repeated»; }
$ nix eval --expr 'let self = { inherit self; foo = 2; }; in self' --json
zsh: segmentation fault  nix eval --expr 'let self = { inherit self; foo = 2; }; in self' --json

This isn't a real segfault, it's obviously just infinite recursion and stack overflow.

Steps To Reproduce

See above

Expected behavior

I am not sure what the strictly correct behaviour here is. Maybe we need a recursion limit and some way to signal infinite recursion. Maybe we need to emit NaNs.

The challenge with this is that we can't standardize that the printer will do the repeated thing exactly the same every time in a stable API.

nix --version output

nix (Lix, like Nix) 2.93.0-dev-pre20250324-4181796
System type: aarch64-darwin
Additional system types: x86_64-darwin
Features: gc, signed-caches
System configuration file: /etc/nix/nix.conf
User configuration files: /Users/jade/.config/nix/nix.conf:/etc/xdg/nix/nix.conf
Store directory: /nix/store
State directory: /nix/var/nix
Data directory: /nix/store/zrvf4zf2jl838kd987ngim3a18jpx6kj-lix-2.93.0-dev-pre20250324-4181796/share

Additional context

Add any other context about the problem here.

## Describe the bug ``` $ nix eval --expr 'let self = { inherit self; foo = 2; }; in self' { foo = 2; self = «repeated»; } $ nix eval --expr 'let self = { inherit self; foo = 2; }; in self' --json zsh: segmentation fault nix eval --expr 'let self = { inherit self; foo = 2; }; in self' --json ``` This isn't a real segfault, it's obviously just infinite recursion and stack overflow. ## Steps To Reproduce See above ## Expected behavior I am not sure what the strictly correct behaviour here is. Maybe we need a recursion limit and some way to signal infinite recursion. Maybe we need to emit NaNs. The challenge with this is that we can't standardize that the printer will do the `repeated` thing exactly the same every time in a stable API. ## `nix --version` output ``` nix (Lix, like Nix) 2.93.0-dev-pre20250324-4181796 System type: aarch64-darwin Additional system types: x86_64-darwin Features: gc, signed-caches System configuration file: /etc/nix/nix.conf User configuration files: /Users/jade/.config/nix/nix.conf:/etc/xdg/nix/nix.conf Store directory: /nix/store State directory: /nix/var/nix Data directory: /nix/store/zrvf4zf2jl838kd987ngim3a18jpx6kj-lix-2.93.0-dev-pre20250324-4181796/share ``` ## Additional context Add any other context about the problem here.
Owner

perhaps we should just disallow circular data in --json by default? we should have some way to allow circular data, perhaps by emitting not an immediately valid json object but an object with "<circular reference $id>" placeholders and a set of json pointer mappings that resolve these references (so as to not accidentally make the reference placeholder strings non-representable)

perhaps we should just disallow circular data in `--json` by default? we should have *some* way to allow circular data, perhaps by emitting not an immediately valid json object but an object with `"<circular reference $id>"` placeholders and a set of json pointer mappings that resolve these references (so as to not accidentally make the reference placeholder strings non-representable)
Author
Owner

I think that's a really smart idea! I'm not blocked on this either way because I just use --apply to remove the problematic attrs.

I think that's a really smart idea! I'm not blocked on this either way because I just use --apply to remove the problematic attrs.
Sign in to join this conversation.
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: lix-project/lix#772
No description provided.