Skip to content

cbuf.h read() and peek() wrong return type! (not int) #12

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

Closed
Links2004 opened this issue Mar 29, 2015 · 3 comments
Closed

cbuf.h read() and peek() wrong return type! (not int) #12

Links2004 opened this issue Mar 29, 2015 · 3 comments

Comments

@Links2004
Copy link
Collaborator

read() and peek() in cbuf.h hast return type char this is wrong!

need to be int
int read()
int peek()

if not readBytesUntil for example is not working.

@igrr
Copy link
Member

igrr commented Mar 29, 2015

What exactly is not working? Could you please post an example sketch?

I think in WiFiClient.cpp I always call getSize() before calling read and peek, so this shouldn't be a problem.

Will change the return type, just wondering why this would be an issue.

@igrr
Copy link
Member

igrr commented Mar 29, 2015

Oh, right, that's HardwareSerial. Forgot to add a check there.
WiFiClient doesn't use cbuf now.
Sure. Will fix.

@Links2004
Copy link
Collaborator Author

example:

char buf[16];
Serial.begin(9600);
int c = Serial.readBytesUntil((char) 0x03, buf, 16); //<- instant return buffer full of gabage (0xFF)

reason:

size_t ICACHE_FLASH_ATTR Stream::readBytesUntil(char terminator, char *buffer, size_t length)
{
  if (length < 1) return 0;
  size_t index = 0;
  while (index < length) {
    int c = timedRead(); // <--- int here
    if (c < 0 || c == terminator) break;
    *buffer++ = (char)c;
    index++;
  }
  return index; // return number of characters, not including null terminator
}
// private method to read stream with timeout
int ICACHE_FLASH_ATTR Stream::timedRead()
{
  int c;
  _startMillis = millis();
  do {
    c = read();  // <--- int here
    if (c >= 0) return c;
    yield();
  } while(millis() - _startMillis < _timeout);
  return -1;     // -1 indicates timeout
}
int ICACHE_FLASH_ATTR HardwareSerial::read(void)
{
    return _rx_buffer->read();  // <--- no int here
}
char read()  // <--- no int here
    {
        if (getSize() == 0)
            return -1;  // <--- no int type cast to char --> result 0xFF

        char result = *_begin;
        if (++_begin == _bufend)
            _begin = _buf;
        return result;
    }

@igrr igrr closed this as completed in 260aee7 Mar 29, 2015
igrr pushed a commit that referenced this issue May 7, 2015
chadouming pushed a commit to chadouming/Arduino that referenced this issue Jul 1, 2015
igrr added a commit that referenced this issue Oct 29, 2015
igrr pushed a commit that referenced this issue Oct 29, 2015
ascillato pushed a commit to ascillato/Arduino that referenced this issue Nov 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants