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

Can't add same element to cleared map #22139

Closed
ratkingsminion opened this issue Aug 31, 2024 · 2 comments · Fixed by #22140
Closed

Can't add same element to cleared map #22139

ratkingsminion opened this issue Aug 31, 2024 · 2 comments · Fixed by #22140
Assignees
Labels
Bug This tag is applied to issues which reports bugs. Modules: builtin Bugs and problems, concerning the builtin types in V - array, maps, strings, runes. Status: Confirmed This bug has been confirmed to be valid by a contributor.

Comments

@ratkingsminion
Copy link

ratkingsminion commented Aug 31, 2024

Describe the bug

Code: https://play.vlang.io/p/b906a74650

mut ints := map[int]int

ints[5] = 5
assert(ints.len == 1)
ints.clear()
assert(ints.len == 0)
ints[3] = 3
assert(ints.len == 1)
ints.clear()
assert(ints.len == 0)
ints[3] = 3
assert(ints.len == 1) // panics! does not panic if line before is "ints[5] = 5"

Reproduction Steps

Run the code above. It will panic on the last assert()

Expected Behavior

There should be no panic. ints[3] = 3 should add an element to the cleared (empty) map, but it does not.

Current Behavior

Output:

code.v:12: FAIL: fn main.main: assert (ints.len == 1)
V panic: Assertion failed...
v hash: 217b191
/tmp/v_60000/../../../../../../home/admin/v/vlib/builtin/builtin.c.v:136: at _v_panic: Backtrace
/tmp/v_60000/../../../../../../box/code.v:14: by main__main
/tmp/v_60000/../../../../../../tmp/v_60000/code.01J6MJA57JHHGAQJ61AMMGVTY3.tmp.c:26253: by main
Exited with error status 1

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.4.7 217b191

Environment details (OS name and version, etc.)

V full version: V 0.4.7 217b191
OS: linux, Debian GNU/Linux 11 (bullseye) (VM)
Processor: 1 cpus, 64bit, little endian, Intel(R) Xeon(R) CPU E5-2686 v4 @ 2.30GHz

getwd: /home/admin/playground
vexe: /home/admin/v/v
vexe mtime: 2024-08-31 15:25:26

vroot: OK, value: /home/admin/v
VMODULES: OK, value: .vmodules
VTMP: OK, value: /tmp/v_0

Git version: git version 2.30.2
Git vroot status: Error: fatal: detected dubious ownership in repository at '/home/admin/v'
To add an exception for this directory, call:

	git config --global --add safe.directory /home/admin/v
.git/config present: true

CC version: cc (Debian 10.2.1-6) 10.2.1 20210110
thirdparty/tcc status: Error: fatal: detected dubious ownership in repository at '/home/admin/v/thirdparty/tcc'
To add an exception for this directory, call:

	git config --global --add safe.directory /home/admin/v/thirdparty/tcc
 Error: fatal: detected dubious ownership in repository at '/home/admin/v/thirdparty/tcc'
To add an exception for this directory, call:

	git config --global --add safe.directory /home/admin/v/thirdparty/tcc

Note

You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.

@ratkingsminion ratkingsminion added the Bug This tag is applied to issues which reports bugs. label Aug 31, 2024
@ratkingsminion ratkingsminion changed the title Bug with cleared map Can't add same element to cleared map Aug 31, 2024
@JalonSolov
Copy link
Contributor

It doesn't seem to matter what value is set first. The issue appears to be setting the same key/value after the 2nd .clear() call on the map.

@JalonSolov JalonSolov added the Status: Confirmed This bug has been confirmed to be valid by a contributor. label Aug 31, 2024
@spytheman
Copy link
Member

The memory block holding the keys was not cleared.

@@ -317,6 +317,7 @@ pub fn (mut m map) clear() {
        m.len = 0
        m.even_index = 0
        m.key_values.len = 0
+       unsafe { vmemset(m.key_values.keys, 0, m.key_values.key_bytes * m.key_values.cap) }
 }

fixes that problem locally.

@spytheman spytheman self-assigned this Aug 31, 2024
@spytheman spytheman added the Modules: builtin Bugs and problems, concerning the builtin types in V - array, maps, strings, runes. label Aug 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug This tag is applied to issues which reports bugs. Modules: builtin Bugs and problems, concerning the builtin types in V - array, maps, strings, runes. Status: Confirmed This bug has been confirmed to be valid by a contributor.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants