Amazon Web Services SDK

8015.aws_logo_cmyk_183d6be6

Check if the string contains itself

V3062 An object ‘attributeName’ is used as an argument to its own method. Consider checking the first actual argument of the ‘Contains’ method. AWSSDK.MobileAnalytics.Net45 CustomEvent.cs 261

/// 
/// Dictionary that stores attribute for this event only.
/// 
private Dictionary _attributes =
  new Dictionary();

/// 
/// Gets the attribute.
///    
/// Attribute name.
/// The attribute. Return null of attribute doesn't
///          exist.
public string GetAttribute(string attributeName)
{
  if(string.IsNullOrEmpty(attributeName))
  {
    throw new ArgumentNullException("attributeName");
  }
  string ret = null;
  lock(_lock)
  {
    if(attributeName.Contains(attributeName))
      ret = _attributes[attributeName];
  }
  return ret;
}

The analyzer has detected an error in the GetAttribute method: a string is checked whether it contains itself. From the description of the method it follows that if the attribute name (attributeName key) is found (in the dictionary _attributes), the attribute value should be returned, otherwise – null. In fact, as the condition attributeName.Contains(attributeName) is always true, an attempt is made to return the value by a key which might not be found in a dictionary. Then, instead of returning null, an exception KeyNotFoundException will be thrown.

Please click here to see more bugs from this project.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.