Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SmsDetector doesnt check how many bytes where read from input #615

Closed
agilob opened this issue Sep 19, 2015 · 11 comments
Closed

SmsDetector doesnt check how many bytes where read from input #615

agilob opened this issue Sep 19, 2015 · 11 comments
Labels
Milestone

Comments

@agilob
Copy link
Contributor

agilob commented Sep 19, 2015

       while (getSmsDetectionState()) {
            try {
                int bufferlen = dis.available();

                if (bufferlen != 0) {
                    byte[] b = new byte[bufferlen];
                    dis.read(b);

                    String split[] = new String(b).split("\n");
                    checkForSilentSms(split);

                } else {
                    Thread.sleep(1000);
                }

You're reading data from dis and doing nothing with it. It looks like you should do something with the input and this introduces a potential and very difficult to reproduce bug.
You cannot assume that any given stream reading call will fill the byte[] passed in to the method. Instead, you must check the value returned by the read method to see how many bytes were read.

@ghost
Copy link

ghost commented Sep 19, 2015

Forgive me if I wrong but the bytes from this dis.read(b); are stored in the b array and then this is turned into a string as you can see in the next few lines? This string is then split by \n and sent to the sms detection function as an array of strings.

The dis isn't read if it has 0 bytes bufferlen != 0, I should have changed this to > 0 to avoid -1 values.

but this data is being used.

@ghost
Copy link

ghost commented Sep 19, 2015

bufferlen = dis.available(); this is telling how many bytes are available in the stream to read also.

@agilob
Copy link
Contributor Author

agilob commented Sep 19, 2015

It tells you how many bytes are(were) available, but not how many bytes were read, read method might still return -1 if something fails internally.

Returns the number of bytes actually read or -1 if the end of the stream has been reached.

@agilob
Copy link
Contributor Author

agilob commented Sep 19, 2015

also, you just found another serious, difficult to reproduce bug with available() method, according to the same documentation:

Returns an estimated number of bytes that can be read or skipped without blocking for more input.

Note that this method provides such a weak guarantee that it is not very useful in practice.

Thirdly, the fact that a given number of bytes is "available" does not guarantee that a read or skip will actually read or skip that many bytes: they may read or skip fewer.

@ghost
Copy link

ghost commented Sep 19, 2015

Whats the best solution to the problem then ali because so far its working for me continuously on 3 different model phones and doesn't miss an sms being sent?

@agilob
Copy link
Contributor Author

agilob commented Sep 19, 2015

Ignore bufferlength size and check if(dis.read() > 0) instead, I would say, but I dont understand what Thread.sleep(1000) is for.

@ghost
Copy link

ghost commented Sep 19, 2015

Only put sleep to slow things down to save cpu usage and give buffer or stream time to fill up instead of continuously reading ever nano second

@agilob
Copy link
Contributor Author

agilob commented Sep 19, 2015

Ymm ok, so I guess this could be changed to this.sleep() for better readability.
Could you change both sleep() method in this class and check if that still works correctly?

I mean, this might not work as you think it works(?)

  public void run() {
        setSmsDetectionState(true);
        try {
            new Thread().sleep(500);

it constructs new thread, makes it wait 500 before not doing anything else to the thread so it dies in GC.

@SecUpwN SecUpwN added the bug label Sep 20, 2015
@DJaeger
Copy link
Collaborator

DJaeger commented Oct 14, 2015

@banjaxbanto: have you tested @Agilop's proposal?

@rsharipov
Copy link

I believe, if the read method returned value is used instead of available(), it would block until the data is available, so there would be no need for Thread.sleep call to save CPU.

@SecUpwN
Copy link
Member

SecUpwN commented Nov 13, 2015

Thanks for your endurance with our shitty bugs, @banjaxbanjo. I am currently cleaning up. Stay tight!

SecUpwN added a commit that referenced this issue Nov 20, 2015
Cleaned up SmsDetector.java implementation, related issues #615, #537
@smarek smarek closed this as completed Nov 20, 2015
@smarek smarek added this to the v0.1.37-alpha milestone Nov 20, 2015
@smarek smarek self-assigned this Nov 20, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants