[Git][NTPsec/ntpsec][master] 2 commits: refclock_gpsd: Cleanup log_data().
Gary E. Miller
gitlab at mg.gitlab.com
Sun Mar 3 00:19:59 UTC 2019
Gary E. Miller pushed to branch master at NTPsec / ntpsec
Commits:
e889a932 by Gary E. Miller at 2019-03-03T00:19:45Z
refclock_gpsd: Cleanup log_data().
Replace static buffer with a larger buffer on the stack.
Remove unneeded add_string().
Prevent buffer overflow.
- - - - -
8196a672 by Gary E. Miller at 2019-03-03T00:19:45Z
refclock_gpsd: Cleanup save_ltc().
More strlcpy() magic.
- - - - -
1 changed file:
- ntpd/refclock_gpsd.c
Changes:
=====================================
ntpd/refclock_gpsd.c
=====================================
@@ -2072,14 +2072,12 @@ save_ltc(
clockprocT * const pp,
const char * const tc)
{
- size_t len;
- len = (tc) ? strlen(tc) : 0;
- if (len >= sizeof(pp->a_lastcode))
- len = sizeof(pp->a_lastcode) - 1;
- pp->lencode = (unsigned short)len;
- memcpy(pp->a_lastcode, tc, len);
- pp->a_lastcode[len] = '\0';
+ if (NULL == tc) {
+ pp->a_lastcode[0] = '\0';
+ } else {
+ strlcpy(pp->a_lastcode, tc, sizeof(pp->a_lastcode));
+ }
}
/* -------------------------------------------------------------------
@@ -2114,19 +2112,10 @@ myasprintf(
/* -------------------------------------------------------------------
* dump a raw data buffer
+ *
+ * Maybe this could be used system wide?
*/
-static char *
-add_string(
- char *dp,
- char *ep,
- const char *sp)
-{
- while (dp != ep && *sp)
- *dp++ = *sp++;
- return dp;
-}
-
static void
log_data(
peerT *peer,
@@ -2134,8 +2123,7 @@ log_data(
const char *buf ,
size_t len )
{
- /* we're running single threaded with regards to the clocks. */
- static char s_lbuf[2048];
+ char s_lbuf[MAX_PDU_LEN];
clockprocT * const pp = peer->procptr;
gpsd_unitT * const up = (gpsd_unitT *)pp->unitptr;
@@ -2144,17 +2132,20 @@ log_data(
const char *sptr = buf;
const char *stop = buf + len;
char *dptr = s_lbuf;
- char *dtop = s_lbuf + sizeof(s_lbuf) - 1; /* for NUL */
+ /* leave room for hex (\\x23) + NUL */
+ char *dtop = s_lbuf + sizeof(s_lbuf) - 10;
- while (sptr != stop && dptr != dtop) {
+ while (sptr != stop && dptr < dtop) {
if (*sptr == '\\') {
- dptr = add_string(dptr, dtop, "\\\\");
+ /* replace with two \ */
+ *dptr++ = '\\';
+ *dptr++ = '\\';
} else if (isprint(*sptr)) {
*dptr++ = *sptr;
} else {
- char fbuf[6];
- snprintf(fbuf, sizeof(fbuf), "\\%03o", *(const uint8_t*)sptr);
- dptr = add_string(dptr, dtop, fbuf);
+ dptr += snprintf(dptr, dtop - dptr, "\\%#.2x",
+ *(const uint8_t*)sptr);
+
}
sptr++;
}
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/compare/69977b249f90c066a6d22bb140f9e30c4f9abc63...8196a672eb4d3df982db8626d2d408fe7d72100a
--
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/compare/69977b249f90c066a6d22bb140f9e30c4f9abc63...8196a672eb4d3df982db8626d2d408fe7d72100a
You're receiving this email because of your account on gitlab.com.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ntpsec.org/pipermail/vc/attachments/20190303/6e95c6e6/attachment-0001.html>
More information about the vc
mailing list