johnatm / itween

Automatically exported from code.google.com/p/itween
1 stars 2 forks source link

Possible bug in iTween.Count(GameObject target, string type) #109

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. Start a tween with a short type name (such as ValueTo) running on a 
gameObject.
2. Before the first tween has completed. Call iTween.Count(gameObject, 
"ShakePosition") (note this type name is longer than ValueTo.

What is the expected output?
A count value of 0 should be returned

What do you see instead?
The following error is thrown -

ArgumentOutOfRangeException: startIndex + length > this.length
Parameter name: length
System.String.Substring (Int32 startIndex, Int32 length)
iTween.Count (UnityEngine.GameObject target, System.String type) (at 
Assets/Plugins/iTween/Plugins/iTween.cs:6343)

What version of the product are you using? On what operating system?
iTween 2.0.45 OS X.

Please provide any additional information below.

The error is caused because the string "ShakePosition" is longer than the 
running tween "ValueFloat". A check should be made before the SubString method 
is called to make sure that the length is not out of range.

This should fix it:

    public static int Count(GameObject target, string type){
        int tweenCount = 0;
        Component[] tweens = target.GetComponents(typeof(iTween));foreach (iTween item in tweens){
            string targetType = item.type+item.method;
            if(type.Length <= targetType.Length)
            {
                targetType=targetType.Substring(0,type.Length);
                if(targetType.ToLower() == type.ToLower()){
                    tweenCount++;
                }
            }
        }
        return(tweenCount);
    }   

It looks like this same error may occur in other methods where 
targetType=targetType.Substring(0,type.Length) is used.

Original issue reported on code.google.com by retrow...@gmail.com on 29 Jul 2012 at 7:57

GoogleCodeExporter commented 8 years ago
I ran into this same issue. I'm currently working around it by using three 
letter abbreviations for my tween types.

Original comment by j.to.the...@gmail.com on 3 Jan 2013 at 10:44

GoogleCodeExporter commented 8 years ago
I didn't like the work around, so I created a GitHub repository and fixed it 
there (https://github.com/jtothebell/iTween if anyone else is interested in 
this fix or fixing other bugs there)

Original comment by j.to.the...@gmail.com on 3 Jan 2013 at 11:40

GoogleCodeExporter commented 8 years ago
I would just like to note, there are several other places in iTween where this 
check before calling Substring() should be included to avoid exceptions. See: 
Resume(), Pause() & Stop().

Original comment by ewersp on 28 Jul 2014 at 6:00