public override void Bad() { float data; data = float.MinValue; /* Initialize data */ { File.Create("data.txt").Close(); StreamReader sr = null; try { /* read string from file into data */ sr = new StreamReader("data.txt"); /* FLAW: Read data from a file */ /* This will be reading the first "line" of the file, which * could be very long if there are little or no newlines in the file */ string stringNumber = sr.ReadLine(); if (stringNumber != null) /* avoid NPD incidental warnings */ { try { data = float.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 { /* Close stream reading objects */ try { if (sr != null) { sr.Close(); } } catch (IOException exceptIO) { IO.Logger.Log(NLog.LogLevel.Warn, exceptIO, "Error closing StreamReader"); } } } LinkedList <float> dataLinkedList = new LinkedList <float>(); dataLinkedList.AddLast(data); dataLinkedList.AddLast(data); dataLinkedList.AddLast(data); CWE197_Numeric_Truncation_Error__float_File_to_byte_73b.BadSink(dataLinkedList); }
/* goodG2B() - use goodsource and badsink */ private static void GoodG2B() { float data; /* FIX: Use a hardcoded number that won't cause underflow, overflow, divide by zero, or loss-of-precision issues */ data = 2; LinkedList <float> dataLinkedList = new LinkedList <float>(); dataLinkedList.AddLast(data); dataLinkedList.AddLast(data); dataLinkedList.AddLast(data); CWE197_Numeric_Truncation_Error__float_File_to_byte_73b.GoodG2BSink(dataLinkedList); }