public override void Bad() { float data; data = -1.0f; /* Initialize data */ /* Read data using a listening tcp connection */ { TcpListener listener = null; /* Read data using a listening tcp connection */ try { listener = new TcpListener(IPAddress.Parse("10.10.1.10"), 39543); using (TcpClient tcpConn = listener.AcceptTcpClient()) { /* read input from socket */ using (StreamReader sr = new StreamReader(tcpConn.GetStream())) { /* POTENTIAL FLAW: Read data using a listening tcp connection */ string stringNumber = sr.ReadLine(); if (stringNumber != null) // avoid NPD incidental warnings { try { data = int.Parse(stringNumber.Trim()); } catch (FormatException exceptNumberFormat) { IO.Logger.Log(NLog.LogLevel.Warn, exceptNumberFormat, "Number format exception parsing data from string"); } } } } } catch (IOException exceptIO) { IO.Logger.Log(NLog.LogLevel.Warn, exceptIO, "Error with stream reading"); } finally { try { listener.Stop(); } catch (SocketException se) { IO.Logger.Log(NLog.LogLevel.Warn, se, "Problem closing socket"); } } } Dictionary <int, float> dataDictionary = new Dictionary <int, float>(); dataDictionary.Add(0, data); dataDictionary.Add(1, data); dataDictionary.Add(2, data); CWE369_Divide_by_Zero__float_listen_tcp_divide_74b.BadSink(dataDictionary); }
/* goodG2B() - use GoodSource and BadSink */ private static void GoodG2B() { float data; /* FIX: Use a hardcoded number that won't a divide by zero */ data = 2.0f; Dictionary <int, float> dataDictionary = new Dictionary <int, float>(); dataDictionary.Add(0, data); dataDictionary.Add(1, data); dataDictionary.Add(2, data); CWE369_Divide_by_Zero__float_listen_tcp_divide_74b.GoodG2BSink(dataDictionary); }