select2 icon indicating copy to clipboard operation
select2 copied to clipboard

"Maximum call stack size exceeded" due to incorrect "arguments" in jquery-select2

Open rdunlop opened this issue 10 years ago • 9 comments

When using .select2("val") in a particular scenario, I can cause a "Maximum call stack size exceeded".

Note: this same scenario was in use in v4.0.0 without issue.

Prerequisites

  • [x] I have searched for similar issues in both open and closed tickets and cannot find a duplicate
  • [x] The issue still exists against the latest master branch of Select2
  • [x] This is not a usage question (Those should be directed to the community)
  • [x] I have attempted to find the simplest possible steps to reproduce the issue
  • [ ] I have included a failing test as a pull request (Optional)

Steps to reproduce the issue

Simple Select box:

<select id="recursive_break">
  <option value="AL">Alabama</option>
  <option value="IL">Illinois</option>
</select>

Simple JavaScript:

select_element = $("#recursive_break");
$(select_element).on("change", function() {
  // This doesn't work:
  console.log("current selection " + $(select_element).select2("val"));

  // this is the new, recommended way to do it
  console.log("current selection " + select_element.val());
 });
// Initialize select
select_element.select2();

// Simulate choosing an element
select_element.val("IL").trigger("change");

Expected behavior and actual behavior

When I follow those steps, I see a Failure/Error: RangeError: Maximum call stack size exceeded error.

I was expecting "current selection AL"

Environment

(no other browsers were tested) Browsers

  • [x] Google Chrome
  • [ ] Mozilla Firefox
  • [ ] Internet Explorer

Operating System

  • [ ] Windows
  • [x] Mac OS X
  • [ ] Linux
  • [ ] Mobile

Libraries

  • jQuery version: 1.12.0
  • Select2 version: 4.0.2

Isolating the problem

  • [ ] This bug happens on the examples page
  • [ ] The bug happens consistently across all tested browsers
  • [i] This bug happens when using Select2 without other pluigns
  • [x] I can reproduce this bug in a jsfiddle

Solution to the problem:

What concerns me, is that this used to work on version 4.0.0, but broke during the 4.0.1 release. Specific breaking commit is https://github.com/select2/select2/commit/878bb3ab0792197ae74b06f0e8cccb17ef16530e

The problem is that the var args = Array.prototype.slice.call(arguments, 1); call is being performed within the this.each(function(), and thus the arguments value is actually different than expected.

To resolve this, we should move the var args = Array.prototype.slice.call(arguments, 1); outside of the this.each call, and this will resolve the problem.

Another solution to resolve this problem is to change my client-side code to use the underlying .val() method instead of using the .select2('val'), but, as mentioned above, this is a regression from the behavior which used to work in v4.0.0, and the .select2("val") method hasn't been removed yet.

rdunlop avatar Mar 10 '16 02:03 rdunlop

Once updating to Chrome 53.02785.116 this issue has popped up but only with select boxes with a large number of options (2000+ in my case). Simply calling $("#XXX").select2(); on it results in "Maximum call stack size exceeded".

Prior to 53.x it wasn't an issue.

jdixon-86 avatar Sep 22 '16 16:09 jdixon-86

I found out when I removed Parsley it didn't cause an issue with Select2.

jdixon-86 avatar Sep 22 '16 17:09 jdixon-86

@KnowMoreIT , do you have a good solution to handle parsley and select2?

sshd123 avatar Sep 23 '16 10:09 sshd123

I got it working by not using the data-parsley-validate on the form element and instead initialized parsley via javascript AFTER select2.

jdixon-86 avatar Sep 23 '16 17:09 jdixon-86

thx, I'm not sure if it's a bug from the latest Chrome.

sshd123 avatar Sep 24 '16 01:09 sshd123

I had a client report it to me and I noticed the section of my application they were in was not reproducing the same issue to me. I looked at my Chrome version and it was older (not sure which version) so I updated my Chrome and the issue started occurring. So in my case the issue was introduced with the latest Chrome version.

jdixon-86 avatar Sep 24 '16 01:09 jdixon-86

yes, in my case, everything work well in chrome49.(this chrome version installed on my virtualbox win7 os), so i guess it's a bug from latest chrome. i can downgrade chrome version to 52. and test this issue later.

sshd123 avatar Sep 24 '16 03:09 sshd123

Experiencing the same issue as with @KnowMoreIT. Thanks, @KnowMoreIT.

Browser: Chrome version 58.0.3029.96 (64-bit).

robertsky avatar May 09 '17 10:05 robertsky

I got the solution :) problem will solve if write code like this

<script>
// first initialize select2
$("#holiday_type").select2();

// then call parsley
$(".attendance-form").parsley();

</script>

eliyas5044 avatar Jun 10 '17 06:06 eliyas5044