Skip to content

Segmentation fault in list.remove #1701

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

Open
virendrakabra14 opened this issue Apr 12, 2023 · 3 comments
Open

Segmentation fault in list.remove #1701

virendrakabra14 opened this issue Apr 12, 2023 · 3 comments

Comments

@virendrakabra14
Copy link
Contributor

With an element not present in the list, the error-message printing of list.remove segfaults.

from lpython import i32, f64

def test_list_remove():
    list1: list[tuple[i32,f64]] = [(1,2.0)]
    # list1.remove((2,2.0)) # ValueError: The list does not contain the element: -698939408

    list2: list[tuple[i32, str, f64]] = [(1,'a',2.0)]
    list2.remove((2,'a',2.0)) # Segmentation Fault

test_list_remove()

This appears to be due to the %d in ("ValueError: %s%d\n") here (assumes an integer).

I think the solution is to split this into cases, but I am not exactly sure on how to handle item being list, tuple, etc.

@czgdp1807
Copy link
Collaborator

I guess if an element is not present in the list then remove shouldn't do anything to the list, but Python gives me the following error,

Python 3.9.6 (default, Mar 10 2023, 20:16:38) 
[Clang 14.0.3 (clang-1403.0.22.14.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> l = [1, 2, 3]
>>> l.remove(4)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list

@czgdp1807
Copy link
Collaborator

This appears to be due to the %d in ("ValueError: %s%d\n") here (assumes an integer).

You can follow the same pattern as done by Python. Checked with Python 3.10.4 and its still the same. We can worry about beautification later (nice to have for usability but something which should happen later).

@virendrakabra14
Copy link
Contributor Author

You can follow the same pattern as done by Python. Checked with Python 3.10.4 and its still the same. We can worry about beautification later (nice to have for usability but something which should happen later).

Agreed.

I tried the suggested change (to just print x). This resolves the first error, but the second (segfault) persists.

I think this is due to control going ahead of the find_item_position call in remove or index, even if the element was not found. Not sure why this is happening, since we call exit in the former.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants