Java Reference
In-Depth Information
// Set up the BufferedReader br
String line;
// ...
line = br.readLine();
assert line != null;
Becauseinputavailabilitydependsontheuserandcanbeexhaustedatanypointduring
program execution, a robust program must be prepared to gracefully handle and recover
from the unavailability of input. However, using the assert statement to verify that some
significant input was available is inappropriate because it might lead to an abrupt termin-
ation of the process, resulting in a denial of service.
Compliant Solution
Thiscompliantsolutiondemonstratestherecommendedwaytodetectandhandleunavail-
ability of input:
Click here to view code image
BufferedReader br;
// Set up the BufferedReader br
String line;
// ...
line = br.readLine();
if (line == null) {
// Handle error
}
Applicability
Assertions are a valuable diagnostic tool for finding and eliminating software defects that
mayresultinvulnerabilities. Theabsenceofassertions, however,doesnotmeanthatcode
is bug-free.
Search WWH ::




Custom Search