Commit 03f5e460 authored by Brickner_cp's avatar Brickner_cp

Code Analysis

parent 0a706f68
......@@ -18,6 +18,9 @@ namespace PcapDotNet.Base
/// <returns>An array of a given size with elements generated by the given delegate.</returns>
public static T[] GenerateArray<T>(this Func<T> generator, int size)
{
if (generator == null)
throw new ArgumentNullException("generator");
T[] array = new T[size];
for (int i = 0; i != size; ++i)
array[i] = generator.Invoke();
......
......@@ -18,6 +18,9 @@ namespace PcapDotNet.Base
/// </summary>
public static bool IsEmpty<T>(this IEnumerable<T> sequence)
{
if (sequence == null)
throw new ArgumentNullException("sequence");
return !sequence.GetEnumerator().MoveNext();
}
......@@ -44,6 +47,9 @@ namespace PcapDotNet.Base
/// <returns>A string of all the elements.</returns>
public static string SequenceToString<T>(this IEnumerable<T> sequence, string separator, string prefix, string suffix)
{
if (sequence == null)
throw new ArgumentNullException("sequence");
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append(prefix);
bool isFirst = true;
......
using System;
using System.Reflection;
namespace PcapDotNet.Base
......@@ -12,6 +13,9 @@ namespace PcapDotNet.Base
/// </summary>
public static object GetValue(this PropertyInfo propertyInfo, object instanceWithProperty)
{
if (propertyInfo == null)
throw new ArgumentNullException("propertyInfo");
return propertyInfo.GetValue(instanceWithProperty, null);
}
}
......
......@@ -33,6 +33,9 @@ namespace PcapDotNet.Base
/// <returns>A sequence of custom attributes applied to this member, or a sequence with zero (0) elements if no attributes have been applied.</returns>
public static IEnumerable<T> GetCustomAttributes<T>(this MemberInfo memberInfo, bool inherit) where T : Attribute
{
if (memberInfo == null)
throw new ArgumentNullException("memberInfo");
return memberInfo.GetCustomAttributes(typeof(T), inherit).Cast<T>();
}
}
......
......@@ -71,6 +71,9 @@ namespace PcapDotNet.Base
/// <returns>A 128-bit unsigned integer equivalent to the number specified in s.</returns>
public static UInt128 Parse(string value, NumberStyles style, IFormatProvider provider)
{
if (value == null)
throw new ArgumentNullException("value");
if (style != NumberStyles.HexNumber)
throw new NotSupportedException("Only " + NumberStyles.HexNumber + " style is supported");
......
......@@ -130,7 +130,7 @@ namespace PcapDotNet.Core.Test
TestReceivePackets(NumPacketsToSend, NumPacketsToSend / 2, int.MaxValue, 2, PacketSize, PacketCommunicatorReceiveResult.Ok, NumPacketsToSend / 2, 0, 0.02);
// Wait for more packets
TestReceivePackets(NumPacketsToSend, 0, int.MaxValue, 2, PacketSize, PacketCommunicatorReceiveResult.None, NumPacketsToSend, 2, 2.043);
TestReceivePackets(NumPacketsToSend, 0, int.MaxValue, 2, PacketSize, PacketCommunicatorReceiveResult.None, NumPacketsToSend, 2, 2.055);
TestReceivePackets(NumPacketsToSend, -1, int.MaxValue, 2, PacketSize, PacketCommunicatorReceiveResult.None, NumPacketsToSend, 2, 2.3);
TestReceivePackets(NumPacketsToSend, NumPacketsToSend + 1, int.MaxValue, 2, PacketSize, PacketCommunicatorReceiveResult.None, NumPacketsToSend, 2, 2.051);
......
......@@ -22,6 +22,9 @@ BerkeleyPacketFilter::BerkeleyPacketFilter(String^ filterValue, int snapshotLeng
bool BerkeleyPacketFilter::Test([Out] int% snapshotLength, Packet^ packet)
{
if (packet == nullptr)
throw gcnew ArgumentNullException("packet");
pcap_pkthdr pcapHeader;
PacketHeader::GetPcapHeader(pcapHeader, packet);
pin_ptr<Byte> unmanagedPacketBytes = &packet->Buffer[0];
......
......@@ -2,6 +2,7 @@
#include "Pcap.h"
using namespace System;
using namespace PcapDotNet::Core;
LivePacketCommunicator::LivePacketCommunicator(const char* source, int snapshotLength, PacketDeviceOpenAttributes attributes, int readTimeout, pcap_rmtauth* auth, SocketAddress^ netmask)
......@@ -21,5 +22,8 @@ PacketTotalStatistics^ LivePacketCommunicator::TotalStatistics::get()
void LivePacketCommunicator::Transmit(PacketSendBuffer^ sendBuffer, bool isSync)
{
sendBuffer->Transmit(PcapDescriptor, isSync);
if (sendBuffer == nullptr)
throw gcnew ArgumentNullException("sendBuffer");
sendBuffer->Transmit(PcapDescriptor, isSync);
}
......@@ -118,7 +118,10 @@ void PacketCommunicator::SetKernelMinimumBytesToCopy(int size)
void PacketCommunicator::SetSamplingMethod(SamplingMethod^ method)
{
pcap_samp* pcapSamplingMethod = pcap_setsampling(_pcapDescriptor);
if (method == nullptr)
throw gcnew ArgumentNullException("method");
pcap_samp* pcapSamplingMethod = pcap_setsampling(_pcapDescriptor);
pcapSamplingMethod->method = method->Method;
pcapSamplingMethod->value = method->Value;
}
......@@ -239,7 +242,10 @@ void PacketCommunicator::Break()
void PacketCommunicator::SendPacket(Packet^ packet)
{
if (packet->Length == 0)
if (packet == nullptr)
throw gcnew ArgumentNullException("packet");
if (packet->Length == 0)
return;
pin_ptr<Byte> unamangedPacketBytes = &packet->Buffer[0];
if (pcap_sendpacket(_pcapDescriptor, unamangedPacketBytes, packet->Length) != 0)
......@@ -253,7 +259,10 @@ BerkeleyPacketFilter^ PacketCommunicator::CreateFilter(String^ filterValue)
void PacketCommunicator::SetFilter(BerkeleyPacketFilter^ filter)
{
filter->SetFilter(_pcapDescriptor);
if (filter == nullptr)
throw gcnew ArgumentNullException("filter");
filter->SetFilter(_pcapDescriptor);
}
void PacketCommunicator::SetFilter(String^ filterValue)
......
......@@ -13,6 +13,9 @@ using namespace PcapDotNet::Packets;
// static
void PacketDumpFile::Dump(String^ fileName, PcapDataLink dataLink, int snapshotLength, IEnumerable<Packet^>^ packets)
{
if (packets == nullptr)
throw gcnew ArgumentNullException("packets");
pcap_t* pcapDescriptor = pcap_open_dead(dataLink.Value, snapshotLength);
if (pcapDescriptor == NULL)
throw gcnew InvalidOperationException("Unable to open open a dead capture");
......@@ -40,7 +43,10 @@ void PacketDumpFile::Dump(String^ fileName, PcapDataLink dataLink, int snapshotL
void PacketDumpFile::Dump(Packet^ packet)
{
pcap_pkthdr header;
if (packet == nullptr)
throw gcnew ArgumentNullException("packet");
pcap_pkthdr header;
PacketHeader::GetPcapHeader(header, packet);
std::string unmanagedFilename = MarshalingServices::ManagedToUnmanagedString(_filename);
......
......@@ -14,7 +14,10 @@ PacketSendBuffer::PacketSendBuffer(unsigned int capacity)
void PacketSendBuffer::Enqueue(Packet^ packet)
{
pcap_pkthdr pcapHeader;
if (packet == nullptr)
throw gcnew ArgumentNullException("packet");
pcap_pkthdr pcapHeader;
PacketHeader::GetPcapHeader(pcapHeader, packet);
pin_ptr<Byte> unmanagedPacketBytes = &packet->Buffer[0];
if (pcap_sendqueue_queue(_pcapSendQueue, &pcapHeader, unmanagedPacketBytes) != 0)
......
......@@ -35,6 +35,8 @@ namespace PcapDotNet.Packets
/// <returns>The value read from the buffer.</returns>
public static byte ReadByte(this byte[] buffer, int offset)
{
if (buffer == null)
throw new ArgumentNullException("buffer");
return buffer[offset];
}
......@@ -334,6 +336,9 @@ namespace PcapDotNet.Packets
/// <param name="value">The value to write.</param>
public static void Write(this byte[] buffer, int offset, byte value)
{
if (buffer == null)
throw new ArgumentNullException("buffer");
buffer[offset] = value;
}
......@@ -357,6 +362,11 @@ namespace PcapDotNet.Packets
/// <param name="value">The value to write.</param>
public static void Write(this byte[] buffer, ref int offset, IEnumerable<byte> value)
{
if (buffer == null)
throw new ArgumentNullException("buffer");
if (value == null)
throw new ArgumentNullException("value");
foreach (byte b in value)
buffer.Write(offset++, b);
}
......@@ -514,6 +524,9 @@ namespace PcapDotNet.Packets
/// <param name="value">The value to write.</param>
public static void Write(this byte[] buffer, int offset, Datagram value)
{
if (value == null)
throw new ArgumentNullException("value");
value.Write(buffer, offset);
}
......@@ -525,6 +538,9 @@ namespace PcapDotNet.Packets
/// <param name="value">The value to write.</param>
public static void Write(this byte[] buffer, ref int offset, Datagram value)
{
if (value == null)
throw new ArgumentNullException("value");
value.Write(buffer, offset);
offset += value.Length;
}
......
......@@ -21,8 +21,13 @@ namespace PcapDotNet.Packets
/// </summary>
/// <param name="buffer">The buffer to take as a datagram.</param>
public Datagram(byte[] buffer)
: this(buffer, 0, buffer.Length)
{
if (buffer == null)
throw new ArgumentNullException("buffer");
_buffer = buffer;
_startOffset = 0;
_length = buffer.Length;
}
/// <summary>
......@@ -116,7 +121,7 @@ namespace PcapDotNet.Packets
/// </summary>
public bool Equals(Datagram other)
{
if (Length != other.Length)
if (other == null || Length != other.Length)
return false;
for (int i = 0; i != Length; ++i)
......@@ -296,6 +301,9 @@ namespace PcapDotNet.Packets
/// <returns>A value equals to the sum of all 16 bits big endian values of the given bytes.</returns>
protected static uint Sum16Bits(byte[] buffer, int offset, int length)
{
if (buffer == null)
throw new ArgumentNullException("buffer");
int endOffset = offset + length;
uint sum = 0;
while (offset < endOffset - 1)
......
......@@ -37,6 +37,9 @@ namespace PcapDotNet.Packets.Ethernet
/// <param name="address">The string value in hexadecimal format. Every two digits are separated by a colon.</param>
public MacAddress(string address)
{
if (address == null)
throw new ArgumentNullException("address");
string[] hexes = address.Split(':');
if (hexes.Length != 6)
throw new ArgumentException("Failed parsing " + address + " as mac address. Expected 6 hexes and got " + hexes.Length + " hexes", "address");
......
......@@ -180,6 +180,9 @@ namespace PcapDotNet.Packets.Icmp
/// <returns>An IcmpDatagram according to the Icmp message type.</returns>
public static IcmpDatagram CreateDatagram(byte[] buffer, int offset, int length)
{
if (buffer == null)
throw new ArgumentNullException("buffer");
if (length <= Offset.Type)
return new IcmpUnknownDatagram(buffer, offset, length);
......
......@@ -31,6 +31,9 @@ namespace PcapDotNet.Packets.Igmp
/// </param>
public IgmpGroupRecord(IgmpRecordType recordType, IpV4Address multicastAddress, ReadOnlyCollection<IpV4Address> sourceAddresses, Datagram auxiliaryData)
{
if (auxiliaryData == null)
throw new ArgumentNullException("auxiliaryData");
if (auxiliaryData.Length % 4 != 0)
throw new ArgumentException("Auxiliary data length must divide by 4 and can't be " + auxiliaryData.Length, "auxiliaryData");
......
using System;
using System.Globalization;
using System.Text;
......@@ -47,6 +48,9 @@ namespace PcapDotNet.Packets.IpV4
/// </summary>
public IpV4Address(string value)
{
if (value == null)
throw new ArgumentNullException("value");
string[] values = value.Split('.');
_value = (uint)((byte.Parse(values[0], CultureInfo.InvariantCulture) << 24) +
(byte.Parse(values[1], CultureInfo.InvariantCulture) << 16) +
......
......@@ -188,6 +188,9 @@ namespace PcapDotNet.Packets.IpV4
/// <returns>On success - the complex option read. On failure - null.</returns>
public Option CreateInstance(byte[] buffer, ref int offset, byte valueLength)
{
if (buffer == null)
throw new ArgumentNullException("buffer");
if (valueLength < OptionValueMinimumLength)
return null;
......
......@@ -238,6 +238,9 @@ namespace PcapDotNet.Packets.IpV4
/// <returns>On success - the complex option read. On failure - null.</returns>
public Option CreateInstance(byte[] buffer, ref int offset, byte valueLength)
{
if (buffer == null)
throw new ArgumentNullException("buffer");
if (valueLength != OptionValueLength)
return null;
......
......@@ -109,6 +109,9 @@ namespace PcapDotNet.Packets.IpV4
protected static bool TryRead(out IpV4Address[] addresses, out byte pointedAddressIndex,
byte[] buffer, ref int offset, byte valueLength)
{
if (buffer == null)
throw new ArgumentNullException("buffer");
addresses = null;
pointedAddressIndex = 0;
......
......@@ -42,6 +42,9 @@ namespace PcapDotNet.Packets.IpV6
/// </summary>
public IpV6Address(string value)
{
if (value == null)
throw new ArgumentNullException("value");
string cannonizedValue = value;
// Handle ...:1.2.3.4
......
......@@ -19,6 +19,9 @@ namespace PcapDotNet.Packets
/// </summary>
public static Packet FromHexadecimalString(string value, DateTime timestamp, DataLinkKind dataLink)
{
if (value == null)
throw new ArgumentNullException("value");
byte[] bytes = new byte[value.Length / 2];
for (int i = 0; i < value.Length; i += 2)
......@@ -94,7 +97,7 @@ namespace PcapDotNet.Packets
/// <returns>True iff the packets have equal data.</returns>
public bool Equals(Packet other)
{
return (Length == other.Length && this.SequenceEqual(other));
return (other != null && Length == other.Length && this.SequenceEqual(other));
}
/// <summary>
......
......@@ -73,6 +73,9 @@ namespace PcapDotNet.Packets
/// <param name="layers">The layers to build the packet accordingly and by their order.</param>
public PacketBuilder(params ILayer[] layers)
{
if (layers == null)
throw new ArgumentNullException("layers");
if (layers.Length == 0)
throw new ArgumentException("At least one layer must be given", "layers");
......
......@@ -44,6 +44,9 @@ namespace PcapDotNet.Packets.Transport
public TcpOptionMd5Signature(IList<byte> data)
: base(TcpOptionType.Md5Signature)
{
if (data == null)
throw new ArgumentNullException("data");
if (data.Count != OptionValueLength)
throw new ArgumentException("data must be " + OptionValueLength + " bytes and not " + data.Count + " bytes", "data");
......
......@@ -14,6 +14,5 @@
<Rule Id="CA1710" Action="None" />
<Rule Id="CA2000" Action="None" />
<Rule Id="CA2004" Action="None" />
<Rule Id="CA2204" Action="None" />
</Rules>
</RuleSet>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment