[Git][NTPsec/ntpsec][master] 2 commits: ntpd/ntp_scanner.c: Pacify Coverity CID 656341, uninited var.

Gary E. Miller (@garyedmundsmiller) gitlab at mg.gitlab.com
Sat Sep 26 01:35:00 UTC 2026



Gary E. Miller pushed to branch master at NTPsec / ntpsec


Commits:
ac011669 by Gary E. Miller at 2026-09-25T17:46:56-07:00
ntpd/ntp_scanner.c: Pacify Coverity CID 656341, uninited var.

The initializer value gets overwritten, but coverity does not
understand.

- - - - -
d4fded1d by Gary E. Miller at 2026-09-25T18:31:11-07:00
include/ntp_refclock.h: Make lencode unsigned.

This avoids adding a whole bunch of checks to ensure lencode is not
negative.  Part of Issue #895

- - - - -


8 changed files:

- include/ntp_refclock.h
- ntpd/ntp_scanner.c
- ntpd/refclock_arbiter.c
- ntpd/refclock_hpgps.c
- ntpd/refclock_jjy.c
- ntpd/refclock_spectracom.c
- ntpd/refclock_truetime.c
- ntpd/refclock_zyfer.c


Changes:

=====================================
include/ntp_refclock.h
=====================================
@@ -112,8 +112,8 @@ struct refclockproc {
         unsigned long   nextaction;     // local activity timeout
         void    (*action)(struct peer *);  // timeout callback
 
-        char    a_lastcode[BMAX];  // last timecode received
-        int     lencode;        // length of last timecode
+        char a_lastcode[BMAX];  // last timecode received
+        unsigned lencode;       // length of last timecode
 
         int     year;           // year of eternity
         int     yday;           // day of year


=====================================
ntpd/ntp_scanner.c
=====================================
@@ -433,7 +433,7 @@ bool lex_push_file(const char * path)
 
         if (is_directory(fullpath)) {
             // directory scanning
-            struct dirent **namelist;
+            struct dirent **namelist = NULL;  // pacify coverity
             int basecount;
             int i;
 
@@ -441,6 +441,8 @@ bool lex_push_file(const char * path)
             basecount = scandir(fullpath, &namelist,
                                 conf_enable_filter, dirent_rcmp);
             if (0 > basecount) {
+                 msyslog(LOG_DEBUG, "CONFIG: scandir(%s) failed %s(%d)\n",
+                         fullpath, strerror(errno), errno);
                 return false;
             }
 


=====================================
ntpd/refclock_arbiter.c
=====================================
@@ -313,7 +313,7 @@ arb_receive(
         strlcpy(pp->a_lastcode, tbuf, sizeof(pp->a_lastcode));
         pp->a_lastcode[LENARB - 2] = up->qualchar;
         strlcat(pp->a_lastcode, up->status, sizeof(pp->a_lastcode));
-        pp->lencode = (int)strlen(pp->a_lastcode);
+        pp->lencode = strlen(pp->a_lastcode);
         syncchar = ' ';
         if (sscanf(pp->a_lastcode, "%c%2d %3d %2d:%2d:%2d",
             &syncchar, &pp->year, &pp->yday, &pp->hour,
@@ -439,7 +439,7 @@ arb_poll(
         }
         refclock_receive(peer);
         record_clock_stats(peer, pp->a_lastcode);
-        DPRINT(1, ("arbiter: timecode %d %s\n",
+        DPRINT(1, ("arbiter: timecode %u %s\n",
                    pp->lencode, pp->a_lastcode));
 }
 


=====================================
ntpd/refclock_hpgps.c
=====================================
@@ -275,11 +275,11 @@ void hpgps_receive(struct recvbuf *rbufp)
         *pp->a_lastcode = '\0';
         pp->lencode = refclock_gtlin(rbufp, pp->a_lastcode, BMAX, &rd_timestamp);
 
-        DPRINT(1, ("hpgps: lencode: %d timecode:%s\n",
+        DPRINT(1, ("hpgps: lencode: %u timecode:%s\n",
                    pp->lencode, pp->a_lastcode));
 
 if (HPDEBUG) {
-  printf("HP in:%3d %s\n", pp->lencode, pp->a_lastcode);
+  printf("HP in:%3u %s\n", pp->lencode, pp->a_lastcode);
 }
         /*
          * If there's no characters in the reply, we can quit now
@@ -354,7 +354,8 @@ if (HPDEBUG) {
         if (up->linecnt > 0) {
                 up->linecnt--;
                 // Silently drop whole line if it doesn't fit.
-                if ((int)(pp->lencode + 2) <= (SMAX - (up->lastptr - up->statscrn))) {
+                if ((int)(pp->lencode + 2) <=
+                    (SMAX - (up->lastptr - up->statscrn))) {
                         if ( (up->lastptr != up->statscrn) || (up->linecnt > 0) )
                                 // ID string stays on same line
                                 *up->lastptr++ = '\n';


=====================================
ntpd/refclock_jjy.c
=====================================
@@ -539,8 +539,9 @@ jjy_receive ( struct recvbuf *rbufp )
 
         l_fp    tRecvTimestamp;         // arrival timestamp
         int     rc ;
-        char    *pBuf, sLogText [ MAX_LOGTEXT ] ;
-        int     i, j, iReadRawBuf, iBreakPosition ;
+        char    *pBuf, sLogText [MAX_LOGTEXT];
+        int i, j, iReadRawBuf, iBreakPosition;
+        unsigned u;
 
         /*
          * Initialize pointers and read the timecode and timestamp
@@ -554,34 +555,37 @@ jjy_receive ( struct recvbuf *rbufp )
          */
         if ( up->linediscipline == LDISC_RAW ) {
 
-                pp->lencode  = (int)refclock_gtraw ( rbufp, pp->a_lastcode, BMAX-1, &tRecvTimestamp ) ;
-                // 3rd argument can be BMAX, but the coverity scan tool claim "Memory - corruptions  (OVERRUN)"
-                // "a_lastcode" is defined as "char a_lastcode[BMAX]" in the ntp_refclock.h
-                // To avoid its claim, pass the value BMAX-1.
+                pp->lencode = refclock_gtraw(rbufp, pp->a_lastcode,
+                                             BMAX - 1, &tRecvTimestamp ) ;
+                /* 3rd argument can be BMAX, but the coverity scan tool
+                 * claim "Memory - corruptions  (OVERRUN)"
+                 * "a_lastcode" is defined as "char a_lastcode[BMAX]" in
+                 * the ntp_refclock.h
+                 * To avoid its claim, pass the value BMAX - 1. */
 
                 /*
                  * Append received characters to temporary buffer
                  */
-                for ( i = 0 ;
-                      i < pp->lencode && up->iRawBufLen < MAX_RAWBUF - 2 ;
-                      i ++ , up->iRawBufLen ++ ) {
-                        up->sRawBuf[up->iRawBufLen] = pp->a_lastcode[i] ;
+                for (u = 0 ;
+                     u < pp->lencode &&
+                     up->iRawBufLen < MAX_RAWBUF - 2;
+                     u++ , up->iRawBufLen ++ ) {
+                        up->sRawBuf[up->iRawBufLen] = pp->a_lastcode[u] ;
                 }
                 up->sRawBuf[up->iRawBufLen] = 0 ;
 
 
         } else {
-
-                pp->lencode  = refclock_gtlin ( rbufp, pp->a_lastcode, BMAX, &tRecvTimestamp ) ;
-
+                pp->lencode  = refclock_gtlin (rbufp, pp->a_lastcode,
+                                               BMAX, &tRecvTimestamp);
         }
 #ifdef DEBUG
-        printf( "\nrefclock_jjy.c : %s : Len=%d  ", sFunctionName, pp->lencode ) ;
-        for ( i = 0 ; i < pp->lencode ; i ++ ) {
-                if ( iscntrl( pp->a_lastcode[i] & 0x7F ) ) {
-                        printf("<x%02X>", (unsigned)(pp->a_lastcode[i] & 0xFF));
+        printf( "\nrefclock_jjy.c : %s : Len=%u  ", sFunctionName, pp->lencode);
+        for (u = 0 ; u < pp->lencode; u++ ) {
+                if (iscntrl(pp->a_lastcode[u] & 0x7F ) ) {
+                        printf("<x%02X>", (unsigned)(pp->a_lastcode[u] & 0xFF));
                 } else {
-                        printf( "%c", pp->a_lastcode[i] ) ;
+                        printf( "%c", pp->a_lastcode[u] ) ;
                 }
         }
         printf( "\n" ) ;
@@ -622,60 +626,65 @@ jjy_receive ( struct recvbuf *rbufp )
         for ( ; up->iProcessState == JJY_PROCESS_STATE_RECEIVE ; ) {
 
                 if ( up->linediscipline == LDISC_RAW ) {
-
-                        if ( up->bWaitBreakString ) {
-                                iBreakPosition = getRawDataBreakPosition( up, iReadRawBuf ) ;
-                                if ( iBreakPosition == -1 ) {
-                                        // Break string have not come yet
-                                        if ( up->iRawBufLen < MAX_RAWBUF - 2
-                                          || iReadRawBuf > 0 ) {
-                                                // Temporary buffer is not full
-                                                break ;
-                                        } else {
-                                                // Temporary buffer is full
-                                                iBreakPosition = up->iRawBufLen - 1 ;
-                                        }
+                    if ( up->bWaitBreakString ) {
+                        iBreakPosition = getRawDataBreakPosition(up,
+                                                                 iReadRawBuf);
+                        if ( iBreakPosition == -1 ) {
+                                // Break string have not come yet
+                                if (up->iRawBufLen < MAX_RAWBUF - 2 ||
+                                    iReadRawBuf > 0 ) {
+                                        // Temporary buffer is not full
+                                        break ;
+                                } else {
+                                        // Temporary buffer is full
+                                        iBreakPosition = up->iRawBufLen - 1;
                                 }
-                        } else {
-                                iBreakPosition = up->iRawBufLen - 1 ;
                         }
-
-                        // Copy characters from temporary buffer to process buffer
-                        up->iLineBufLen = up->iTextBufLen = 0 ;
-                        for ( i = iReadRawBuf ; i <= iBreakPosition ; i ++ ) {
-
-                                // Copy all characters
-                                up->sLineBuf[up->iLineBufLen] = up->sRawBuf[i] ;
-                                up->iLineBufLen ++ ;
-
-                                // Copy printable characters
-                                if ( ! iscntrl( (int)up->sRawBuf[i] ) ) {
-                                        up->sTextBuf[up->iTextBufLen] = up->sRawBuf[i] ;
-                                        up->iTextBufLen ++ ;
-                                }
-
+                    } else {
+                        iBreakPosition = up->iRawBufLen - 1;
+                    }
+
+                    /* Copy characters from temporary buffer to
+                     * process buffer */
+                    up->iLineBufLen = up->iTextBufLen = 0 ;
+                    for (i = iReadRawBuf ; i <= iBreakPosition; i++) {
+
+                        // Copy all characters
+                        up->sLineBuf[up->iLineBufLen] = up->sRawBuf[i];
+                        up->iLineBufLen ++ ;
+
+                        // Copy printable characters
+                        if ( ! iscntrl( (int)up->sRawBuf[i] ) ) {
+                                up->sTextBuf[up->iTextBufLen] = up->sRawBuf[i];
+                                up->iTextBufLen ++ ;
                         }
-                        up->sLineBuf[up->iLineBufLen] = 0 ;
-                        up->sTextBuf[up->iTextBufLen] = 0 ;
+
+                    }
+                    up->sLineBuf[up->iLineBufLen] = 0 ;
+                    up->sTextBuf[up->iTextBufLen] = 0 ;
 #ifdef DEBUG
-                        printf( "refclock_jjy.c : %s : up->iLineBufLen=%d up->iTextBufLen=%d\n",
-                                 sFunctionName, up->iLineBufLen, up->iTextBufLen ) ;
+                    printf("refclock_jjy.c : %s : up->iLineBufLen=%d "
+                           " up->iTextBufLen=%d\n",
+                           sFunctionName, up->iLineBufLen, up->iTextBufLen);
 #endif
 
-                        if ( up->bSkipCntrlCharOnly && up->iTextBufLen == 0 ) {
+                    if ( up->bSkipCntrlCharOnly && up->iTextBufLen == 0 ) {
 #ifdef DEBUG
-                                printf( "refclock_jjy.c : %s : Skip cntrl char only : up->iRawBufLen=%d iReadRawBuf=%d iBreakPosition=%d\n",
-                                         sFunctionName, up->iRawBufLen, iReadRawBuf, iBreakPosition ) ;
+                        printf("refclock_jjy.c : %s : Skip cntrl char "
+                               "only : up->iRawBufLen=%d iReadRawBuf=%d "
+                               " iBreakPosition=%d\n",
+                                 sFunctionName, up->iRawBufLen,
+                                 iReadRawBuf, iBreakPosition ) ;
 #endif
-                                if ( iBreakPosition + 1 < up->iRawBufLen ) {
-                                        iReadRawBuf = iBreakPosition + 1 ;
-                                        continue ;
-                                } else {
-                                        break ;
-                                }
-
+                        if ( iBreakPosition + 1 < up->iRawBufLen ) {
+                                iReadRawBuf = iBreakPosition + 1;
+                                continue ;
+                        } else {
+                                break ;
                         }
 
+                    }
+
                 }
 
                 if ( up->linediscipline == LDISC_RAW ) {


=====================================
ntpd/refclock_spectracom.c
=====================================
@@ -226,7 +226,7 @@ spectracom_receive(
         l_fp    trtmp;  // arrival timestamp
         int     tz;  // time zone
         int     day, month;  // ddd conversion
-        int     temp;  // int temp
+        unsigned temp;
         char    syncchar;  // synchronization indicator
         char    qualchar;  // quality indicator
         char    leapchar;  // leap indicator
@@ -514,7 +514,7 @@ spectracom_poll(
 #endif  // HAVE_PPSAPI
         refclock_receive(peer);
         record_clock_stats(peer, pp->a_lastcode);
-        DPRINT(1, ("spectracom: timecode %d %s\n", pp->lencode,
+        DPRINT(1, ("spectracom: timecode %u %s\n", pp->lencode,
                    pp->a_lastcode));
 }
 


=====================================
ntpd/refclock_truetime.c
=====================================
@@ -272,7 +272,7 @@ true_receive(
         // These variables hold data until we decide to keep it
         char    rd_lastcode[BMAX];
         l_fp    rd_tmp;
-        unsigned short  rd_lencode;
+        unsigned rd_lencode;
 
         /*
          * Get the clock this applies to and pointers to the data.
@@ -284,7 +284,7 @@ true_receive(
         /*
          * Read clock output.  Automatically handles CLKLDISC.
          */
-        rd_lencode = (unsigned short)refclock_gtlin(rbufp, rd_lastcode, BMAX, &rd_tmp);
+        rd_lencode = refclock_gtlin(rbufp, rd_lastcode, BMAX, &rd_tmp);
         rd_lastcode[rd_lencode] = '\0';
 
         /*
@@ -292,11 +292,11 @@ true_receive(
          */
         if (rd_lencode == 0) {
                 return;
-}
+        }
         pp->lencode = rd_lencode;
         strlcpy(pp->a_lastcode, rd_lastcode, sizeof(pp->a_lastcode));
         pp->lastrec = rd_tmp;
-        true_debug(peer, "receive(%s) [%d]\n", pp->a_lastcode,
+        true_debug(peer, "receive(%s) [%u]\n", pp->a_lastcode,
                    pp->lencode);
 
         up->pollcnt = 2;


=====================================
ntpd/refclock_zyfer.c
=====================================
@@ -210,8 +210,7 @@ zyfer_receive(
                 else
                         return;
         } else {
-                if (pp->lencode < 0 ||
-                    (size_t)pp->lencode >= sizeof(pp->a_lastcode) - 1) {
+                if ((size_t)pp->lencode >= sizeof(pp->a_lastcode) - 1) {
                     pp->lencode = 0;
                     pp->a_lastcode[0] = '\0';
                     refclock_report(peer, CEVNT_BADREPLY);
@@ -227,7 +226,7 @@ zyfer_receive(
                 }
 
                 memcpy(pp->a_lastcode + pp->lencode, p, rbufp->recv_length);
-                pp->lencode += (int)rbufp->recv_length;
+                pp->lencode += rbufp->recv_length;
                 pp->a_lastcode[pp->lencode] = '\0';
         }
 



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/-/compare/1a1dc6955c49ec2f4329b457ca843e03b158c13a...d4fded1d291e948f0494114309dd91a81a61ee59

-- 
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/-/compare/1a1dc6955c49ec2f4329b457ca843e03b158c13a...d4fded1d291e948f0494114309dd91a81a61ee59
You're receiving this email because of your account on gitlab.com. Manage all notifications: https://gitlab.com/-/profile/notifications | Help: https://gitlab.com/help


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ntpsec.org/pipermail/vc/attachments/20260926/89033146/attachment-0001.htm>


More information about the vc mailing list