From: Sven Eckelmann <sven@narfation.org>
To: b.a.t.m.a.n@lists.open-mesh.org
Cc: Sven Eckelmann <sven@narfation.org>
Subject: [B.A.T.M.A.N.] [PATCHv2 2/2] alfred-gpsd: Avoid underrun when reading from gpsd
Date: Wed, 25 Jun 2014 13:35:58 +0200 [thread overview]
Message-ID: <1403696158-20329-2-git-send-email-sven@narfation.org> (raw)
In-Reply-To: <1403696158-20329-1-git-send-email-sven@narfation.org>
The gpsd output reading function is ignoring \r characters. This is done by
moving the current position (cnt) one position back in the character buffer. It
is jumping to the -1 character (max number for size_t) when it was reading the
first character at position 0. This is not problematic when the cnt is
increased directly after it by 1. Overflows/underflows are defined for
*unsigned* types and thus it just jumps back to 0.
Unfortunatelly, it is trying to access the memory for another check before
increasing the position again. This check is done on memory outside of the
buffer and therefore invalid.
Instead doing two check after each other, it is in this situation better to do
both at once and just handle the current character.
Signed-off-by: Sven Eckelmann <sven@narfation.org>
---
gpsd/alfred-gpsd.c | 9 +++++----
1 file changed, 5 insertions(+), 4 deletions(-)
diff --git a/gpsd/alfred-gpsd.c b/gpsd/alfred-gpsd.c
index d6cdfd6..87943bd 100644
--- a/gpsd/alfred-gpsd.c
+++ b/gpsd/alfred-gpsd.c
@@ -315,15 +315,16 @@ static void gpsd_read_gpsd(struct globals *globals)
return;
}
- if (buf[cnt] == '\r')
+ switch (buf[cnt]) {
+ case '\r':
cnt--;
-
- if (buf[cnt] == '\n') {
+ break;
+ case '\n':
eol = true;
buf[cnt] = '\0';
break;
}
- } while (cnt++ < sizeof(buf) - 1);
+ } while (cnt++ < sizeof(buf) - 1 && !eol);
if (!eol) {
gps_close(&globals->gpsdata);
--
2.0.0
next prev parent reply other threads:[~2014-06-25 11:35 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-06-25 11:35 [B.A.T.M.A.N.] [PATCHv2 1/2] alfred-gpsd: Calculate size of global buffer and not of a single char Sven Eckelmann
2014-06-25 11:35 ` Sven Eckelmann [this message]
2014-06-27 1:58 ` [B.A.T.M.A.N.] [PATCHv2 2/2] alfred-gpsd: Avoid underrun when reading from gpsd Andrew Lunn
2014-06-26 14:40 ` [B.A.T.M.A.N.] [PATCHv2 1/2] alfred-gpsd: Calculate size of global buffer and not of a single char Simon Wunderlich
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1403696158-20329-2-git-send-email-sven@narfation.org \
--to=sven@narfation.org \
--cc=b.a.t.m.a.n@lists.open-mesh.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox