Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #32941
| From | Jan Burse <janburse@fastmail.fm> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: java.util.Scanner hasNext returns true, next throws NoSuchElementException |
| Date | 2015-10-27 19:25 +0100 |
| Organization | albasani.net |
| Message-ID | <n0ofj0$6p3$1@news.albasani.net> (permalink) |
| References | <f0815dfd-0228-4a1a-af8d-40034de9aeb1@googlegroups.com> |
Hi,
I see more tasks for program verification!
Bye
Stories such as:
http://envisage-project.eu/proving-android-java-and-python-sorting-algorithm-is-broken-and-how-to-fix-it
lndresnick@gmail.com schrieb:
> Debugging an exception in my code, found out that under certain conditions java.util.Scanner hasNext() returns true, but next() throws NoSuchElementException. I submitted a bug against java to Oracle for this today, but throwing it out here so folks searching can find. Very happy to hear if I'm wrong, any comments welcome. But the documented interface is that if hasNext() returns true, there is a token available. For next(), the documented interface is that if there is no token available, NoSuchElementException is thrown. In the code below, NoSuchElementException is thrown for input lengths of 1019, 1022, 2046, 4094, and 8190 (plus added delimiter). This bug appears to be triggered if a fuzzy matched delimiter crosses the internal java.util.Scanner buffer size of 1024
>
> Easy enough to work around, mind you. Just catch the exception locally and consider it "success".
>
> Code follows:
>
> import java.util.Scanner;
> import java.util.NoSuchElementException;
>
> public class Boundary {
>
> static private boolean parse(String data, String boundary) {
> String dataCopy = new String(data);
> try {
> Scanner scanner = new Scanner(dataCopy);
> try {
> scanner.useDelimiter("-" + boundary + "(-)?");
>
> while (scanner.hasNext()) {
> String bodyPart = scanner.next();
> }
> } finally {
> if (scanner != null)
> scanner.close();
> }
> } catch (NoSuchElementException e) {
> System.out.println("Caught NoSuchElementException " + e);
> e.printStackTrace();
> return false;
> }
>
> return true;
> }
>
> public static void main (String argv[]) {
> final String boundary = ";";
>
> for ( int i = 0; i < 10000; ++i ) {
> StringBuilder data = new StringBuilder();
> data.append("--" + boundary);
> for (int j = 0; j< i; ++j) {
> data.append(j%10);
> }
>
> data.append("-" + boundary + "-");
>
> boolean ret=parse(data.toString(), boundary);
> if (!ret) {
> System.out.println("parse returned " + ret + " i=" +i);
> }
> }
> }
> }
>
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Find similar | Unroll thread
java.util.Scanner hasNext returns true, next throws NoSuchElementException lndresnick@gmail.com - 2015-10-09 07:12 -0700
Re: java.util.Scanner hasNext returns true, next throws NoSuchElementException Jeff Higgins <jeff@invalid.invalid> - 2015-10-09 11:16 -0400
Re: java.util.Scanner hasNext returns true, next throws NoSuchElementException lndresnick@gmail.com - 2015-10-09 08:29 -0700
Re: java.util.Scanner hasNext returns true, next throws NoSuchElementException lndresnick@gmail.com - 2015-10-27 09:57 -0700
Re: java.util.Scanner hasNext returns true, next throws NoSuchElementException Jan Burse <janburse@fastmail.fm> - 2015-10-27 19:25 +0100
csiph-web