Amazon Web Services SDK


BUG OF THE MONTH | 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)
    throw new ArgumentNullException("attributeName");
  string ret = null;
      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: Logo

You are commenting using your 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.