Search code examples
javabluemix-app-scan

How to handle CWE-400-Resource exhaustion error


We are getting an IBM APPSCAN exception for the following code.

{
    br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
}
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
    sb.append(line+"\n");
}
br.close(); 

Can someone suggest a way to handle the same.


Solution

  • I myself have figured out the solution for this.

    Just we need to limit the character read by readline(). there is no way to limit the same, so we need to use BoundedBufferedReader.

    Try the below:

    {
        br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
    }
    StringBuilder sb = new StringBuilder();
    String line;
    BoundedBufferedReader boundedReader = new BoundedBufferedReader(br,204800,204800);
                       while (( line = boundedReader.readLine() ) != null) {
                           sb.append(line+"\n");
                       }
    br.close();