summaryrefslogtreecommitdiff
path: root/hex.c
diff options
context:
space:
mode:
authorEric S. Raymond <esr@thyrsus.com>2013-10-13 22:28:47 -0400
committerEric S. Raymond <esr@thyrsus.com>2013-10-13 22:28:47 -0400
commit7fe72f79f289166b3b23947a198a3f511bf5bf52 (patch)
tree621c9823f18cc86825d23a11d6b6299e35041508 /hex.c
parent5ae819b8e4b9df0da24953355d6d05b9001a82a9 (diff)
downloadgpsd-7fe72f79f289166b3b23947a198a3f511bf5bf52.tar.gz
Make all hex-dumping truly thread-safe.
Diffstat (limited to 'hex.c')
-rw-r--r--hex.c26
1 files changed, 15 insertions, 11 deletions
diff --git a/hex.c b/hex.c
index c4130a66..95f4121d 100644
--- a/hex.c
+++ b/hex.c
@@ -8,7 +8,9 @@
#include "gpsd.h"
-const char /*@ observer @*/ *gpsd_packetdump(char *binbuf, size_t binbuflen)
+/*@-mustdefine@*/
+const char /*@ observer @*/ *gpsd_packetdump(char *scbuf, size_t scbuflen,
+ char *binbuf, size_t binbuflen)
{
char *cp;
bool printable = true;
@@ -20,13 +22,14 @@ const char /*@ observer @*/ *gpsd_packetdump(char *binbuf, size_t binbuflen)
if (printable)
return binbuf;
else
- return gpsd_hexdump(binbuf, binbuflen);
+ return gpsd_hexdump(scbuf, scbuflen, binbuf, binbuflen);
}
+/*@+mustdefine@*/
-const char /*@ observer @*/ *gpsd_hexdump(char *binbuf, size_t binbuflen)
+/*@-mustdefine@*/
+const char /*@ observer @*/ *gpsd_hexdump(char *scbuf, size_t scbuflen,
+ char *binbuf, size_t binbuflen)
{
- /* FIXME: this isn't thead-safe! */
- static char hexbuf[MAX_PACKET_LENGTH * 2 + 1];
#ifndef SQUELCH_ENABLE
size_t i, j = 0;
size_t len =
@@ -39,17 +42,18 @@ const char /*@ observer @*/ *gpsd_hexdump(char *binbuf, size_t binbuflen)
return "";
/*@ -shiftimplementation @*/
- for (i = 0; i < len; i++) {
- hexbuf[j++] = hexchar[(ibuf[i] & 0xf0) >> 4];
- hexbuf[j++] = hexchar[ibuf[i] & 0x0f];
+ for (i = 0; i < len && i * 2 < scbuflen - 2; i++) {
+ scbuf[j++] = hexchar[(ibuf[i] & 0xf0) >> 4];
+ scbuf[j++] = hexchar[ibuf[i] & 0x0f];
}
/*@ +shiftimplementation @*/
- hexbuf[j] = '\0';
+ scbuf[j] = '\0';
#else /* SQUELCH defined */
- hexbuf[0] = '\0';
+ scbuf[0] = '\0';
#endif /* SQUELCH_ENABLE */
- return hexbuf;
+ return scbuf;
}
+/*@+mustdefine@*/
/*@ +charint -shiftimplementation @*/
static int hex2bin(const char *s)