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

addAll enters infinite loop #778

Closed
DartBot opened this issue Dec 9, 2011 · 6 comments
Closed

addAll enters infinite loop #778

DartBot opened this issue Dec 9, 2011 · 6 comments
Assignees
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries.

Comments

@DartBot
Copy link

DartBot commented Dec 9, 2011

This issue was originally filed by [email protected]


What steps will reproduce the problem?

  1. Execute this code:
      var x = [1];
      x.addAll(x);

What is the expected output?
x should be [1, 1]

What do you see instead?
The program hangs.

What version of the product are you using? On what operating system?
Dartboard and the VM. OS X.

Please provide any additional information below.
I think you just need to record length before the loop, rather than recomputing it each iteration.

@DartBot
Copy link
Author

DartBot commented Dec 9, 2011

This comment was originally written by [email protected]


Added Area-VM, Triaged labels.

@ghost
Copy link

ghost commented Dec 9, 2011

This is not a VM issue (replicateable in Dartboard and VM) but a Library issue/feature. Library needs to clarify the behavior when an a List attempts addAll on itself. I would assume that we need to check if receiver and collection are the same, in which case we copy the collection before adding it to receiver.


Removed Area-VM label.
Added Area-Library label.

@floitschG
Copy link
Contributor

Yes this is a library issue.
Just checking if the collections are the same is unfortunately not enough (at least not in the general case). Collection.addAll takes an iterable as argument. So the problem is, when the iterator is backed by the collection we are adding to.
We only discussed this shortly in the office, but haven't yet reached a conclusion. One possibility would be to specify addAll as if it was adding into a temporary list first. Implementations could still try to shortcut. In the general case it would be slower though :(

@jmesserly
Copy link

Just hit this bug too.

A few typical ways to prevent these kinds of errors:

  1. throw if mutation happens during iteration. One way to implement is to track a version number in collections and iterators, and throw if version mismatch happens. (Of course, immutable collections can skip this check). This catches a lot of potential programmer errors at the cost of extra field per collection/iterator and more difficult implementation.

2a. check "collection === this" in addAll. It won't catch sophisticated usage like people creating iteratables backed by your list, but it catches the common case and is a simple fix. (IMO, creating a collection backed by a mutable list, and exposing that mutable list is asking for trouble anyway).

2b. check "collection is Collection" first, and if it is, only add items up to "collection.length" This also allows you to pre-grow the destination list first. It's a pattern we should be using in more places (IMO). Like 2a this won't catch all possible errors, but guards against the identity case and is usually more efficient if you pre-grow the array to the desired size.

@floitschG
Copy link
Contributor

If I'm not wrong this is fixed now (Iterators keep track of the List.length now).


Set owner to @lrhn.
Added Fixed label.

@lrhn
Copy link
Member

lrhn commented Feb 10, 2013

We have probably missed some existing iterators that won't detect the modification, but that would be a separate bug.

List iteration should err if the list's length changes during iteration.
Set iteration should err if the set changes during iteration.
Map iteration should err if the map's key-set changes during iteration.
Queue iteration should err if the queue's start or end moves during iteration (i.e., any change).

It hasn't been implemented everywhere yet, though. I hope to spend some time on it next week.

@DartBot DartBot added Type-Defect area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. labels Feb 10, 2013
copybara-service bot pushed a commit that referenced this issue Dec 2, 2022
…, webdev

Revisions updated by `dart tools/rev_sdk_deps.dart`.

collection (https://github.com/dart-lang/collection/compare/efd709f..caf6802):
  caf6802  2022-11-28  Nate Bosch  Tweak docs for split extensions (#256)

fixnum (https://github.com/dart-lang/fixnum/compare/bca3816..62916f2):
  62916f2  2022-11-24  Lasse R.H. Nielsen  Split into separate libraries instead of using parts. (#97)
  14d4827  2022-11-23  Lasse R.H. Nielsen  Add `tryParse` methods. (#96)

http (https://github.com/dart-lang/http/compare/047d6ed..976bd56):
  976bd56  2022-11-28  Kevin Moore  Use latest mono_repo (#832)

protobuf (https://github.com/dart-lang/protobuf/compare/c181573..4f3e328):
  4f3e328  2022-11-30  Devon Carew  Emit imports in sorted order (#778)
  3cc088e  2022-11-28  Kevin Moore  Latest mono_repo (#779)

sse (https://github.com/dart-lang/sse/compare/8d018dd..d396145):
  d396145  2022-11-29  Elliott Brooks (she/her)  Fix Fetch credentials (#69)

stack_trace (https://github.com/dart-lang/stack_trace/compare/dce0013..cf3562e):
  cf3562e  2022-12-01  Devon Carew  blast_repo fixes (#123)

test (https://github.com/dart-lang/test/compare/b25dac9..f3d80a6):
  f3d80a68  2022-11-29  Nate Bosch  Fix missing label and reason after isNotNull (#1797)
  5b1f0075  2022-11-29  Nate Bosch  Use double quotes for test names on windows (#1802)
  986045c4  2022-11-29  Nate Bosch  Temporarily pin to ubuntu 20.04 (#1800)

webdev (https://github.com/dart-lang/webdev/compare/637b406..91b8a19):
  91b8a19  2022-12-01  Elliott Brooks (she/her)  Fix global variable `isInternalBuild` in injected client (#1805)
  7d0810a  2022-11-30  Elliott Brooks (she/her)  Updates the `fixture` package `pubspecs` so it is clear what shouldn't be migrated to null-safety
(#1803)
  acd3f9f  2022-11-30  Elliott Brooks (she/her)  Can debug with the MV3 Dart Debug Extension (#1802)
  1258510  2022-11-29  Elliott Brooks (she/her)  Detect whether the Debug Extension was built for dev or release (#1800)
  b4a23c6  2022-11-29  Elliott Brooks (she/her)  Fix Fetch API implementation (#1801)
  67133df  2022-11-29  Elliott Brooks (she/her)  Pull out debug logging into one file (#1799)
  a395c68  2022-11-28  Elliott Brooks (she/her)  Handle detecting Dart app when tab changes (#1796)
  4fb4328  2022-11-28  Elliott Brooks (she/her)  Authenticate the user when they click on the Dart Debug Extension icon (#1795)

Change-Id: I7beeeb43de4ba514817836ffd4ff6a62b801f2dc
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/273282
Commit-Queue: Devon Carew <[email protected]>
Auto-Submit: Devon Carew <[email protected]>
Reviewed-by: Konstantin Shcheglov <[email protected]>
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries.
Projects
None yet
Development

No branches or pull requests

4 participants