Skip to content

Instantly share code, notes, and snippets.

@micahlmartin
Created June 19, 2012 15:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save micahlmartin/2954744 to your computer and use it in GitHub Desktop.
Save micahlmartin/2954744 to your computer and use it in GitHub Desktop.
Error Example
//Example of a basic bug that causes an infinite loop
public static IEnumerable<Filter> Create(IEnumerable<string> filters)
{
if (filters == null)
throw new ArgumentNullException("filters");
if (filters.Count() == 0)
throw new ArgumentException("At least 1 filter must be specified", "filters");
var filterList = new List<Filter>();
foreach (var filter in filters)
filterList.AddRange(Create(filters)); //<-- Causes infinite loop
return filterList;
}
public static IEnumerable<Filter> Create(string filter)
{
if (string.IsNullOrEmpty(filter))
return Enumerable.Empty<Filter>();
if (SubSelector.IsMatch(filter))
return GetFiltersFromSubSelector(filter);
return new List<Filter> { new Filter(filter, false) };
}
public static IEnumerable<Filter> Create(IEnumerable<string> filters)
{
if (filters == null)
throw new ArgumentNullException("filters");
if (filters.Count() == 0)
throw new ArgumentException("At least 1 filter must be specified", "filters");
var filterList = new List<Filter>();
foreach (var filter in filters)
filterList.AddRange(Create(filters));
return filterList;
}
public static IEnumerable<Filter> Create(string filter)
{
if (string.IsNullOrEmpty(filter))
return Enumerable.Empty<Filter>();
if (SubSelector.IsMatch(filter))
return GetFiltersFromSubSelector(filter);
return new List<Filter> { new Filter(filter, false) };
}
//Simple example of a possible IndexOutOfRangeException
public static string GetReturnAddress(Address value, Address target)
{
var machine = target.Machine;
IPAddress ipAddress;
//see if the target is an IP address, if so, get our own local ip address
if (IPAddress.TryParse(machine, out ipAddress))
{
string myIp = null;
var networkInterfaces = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces();
foreach(var ni in networkInterfaces)
if (ni.OperationalStatus == OperationalStatus.Up && ni.NetworkInterfaceType != NetworkInterfaceType.Loopback)
{
var ipProps = ni.GetIPProperties();
if (ipProps.UnicastAddresses.Count > 0) //<-- checks if there is at least 1 adddress
{
myIp = ipProps.UnicastAddresses[1].Address.ToString(); //<-- Grabs the 2nd address but there may be only one.
break;
}
}
if (myIp == null)
myIp = "127.0.0.1";
return PREFIX_TCP + myIp + PRIVATE + value.Queue;
}
return PREFIX + GetFullPathWithoutPrefix(value);
}
public static string GetReturnAddress(Address value, Address target)
{
var machine = target.Machine;
IPAddress ipAddress;
//see if the target is an IP address, if so, get our own local ip address
if (IPAddress.TryParse(machine, out ipAddress))
{
string myIp = null;
var networkInterfaces = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces();
foreach(var ni in networkInterfaces)
if (ni.OperationalStatus == OperationalStatus.Up && ni.NetworkInterfaceType != NetworkInterfaceType.Loopback)
{
var ipProps = ni.GetIPProperties();
if (ipProps.UnicastAddresses.Count > 0)
{
myIp = ipProps.UnicastAddresses[1].Address.ToString();
break;
}
}
if (myIp == null)
myIp = "127.0.0.1";
return PREFIX_TCP + myIp + PRIVATE + value.Queue;
}
return PREFIX + GetFullPathWithoutPrefix(value);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment