cleaning after llvm static analysing mirror/master
authorLaurent Mazet <mazet@softndesign.org>
Sat, 19 Apr 2025 16:49:15 +0000 (18:49 +0200)
committerLaurent Mazet <mazet@softndesign.org>
Sat, 19 Apr 2025 16:49:15 +0000 (18:49 +0200)
makefile
morep_test.c
morep_valid.c
parse.c

index f9e2c4750be6998131a93b89b32f93bb525706f9..d55bffc6713ef6d7c66a60de6b01a0ba6e2c57f4 100644 (file)
--- a/makefile
+++ b/makefile
@@ -14,7 +14,6 @@ OFLAGS  = -O4 -Os
 #OFLAGS += -malign-double
 CFLAGS += -W -Wall -Wextra -g
 #CFLAGS += -std=c99 -D_XOPEN_SOURCE=500
-#CFLAGS += -std=c11 -D_XOPEN_SOURCE=500
 CFLAGS += -D_XOPEN_SOURCE=500
 CFLAGS += $(OFLAGS) $(INCLUDES) $(OPTIONS)
 LDFLAGS += -g $(LDOPTS) $(OPTIONS)
@@ -40,10 +39,7 @@ endif
 
 # Targets
 
-ALLEXE  =
-ALLEXE += morep_test
-ALLEXE += morep_valid
-ALLEXE += morep_simulator
+ALLEXE  = $(shell for f in *.c; do grep -q '/\*\slinker:' $$f && echo $${f/.c}; done)
 
 SHELL = bash
 
@@ -72,6 +68,11 @@ GETCOMMENTS = awk '/\/\*\s*$(1):/,/\*\// { sub(/.*\/\*\s*$(1):/, ""); sub (/\s*\
 all: depends
        $(MAKE) $(ALLEXE:%=%.exe)
 
+analyze:
+       make purge
+       scan-build make
+       #scan-build -stats make
+
 count:
        wc $(wildcard *.c *.h) $(MAKEFILE_LIST)
 
@@ -197,7 +198,7 @@ valgrind_%: %.exe
 
 ## Phony
 
-.PHONY: all clean count depends gcovs purge tests
+.PHONY: all analyze clean count depends documentation gcovs purge tests
 
 ## Precious
 
index fd785969534354a3f7e50bf8db05cea9aa4b2cd2..f54b25033650aac398b6d4b6ccee26a0b3a7b310 100644 (file)
@@ -103,52 +103,50 @@ int clientline (char *url, uint8_t txmsgtype, char *filename)
     }
 
     /* read file */
-    FILE *fid = stdin;
-    if (filename != NULL) {
-        fid = fopen (filename, "r");
-        if (fid == NULL) {
-            VERBOSE (morep, ERROR, PRINTF ("can't open file '%s' for reading\n", filename));
-            return -1;
-        }
-    }
-
-    while (!feof (fid)) {
-        int txmsglen = MOREP_PAYLOAD;
-        uint8_t txbuffer[MOREP_PAYLOAD] = {0};
-        int i = 0;
-        do {
-            fread (txbuffer + i, 1, 1, fid);
-            if ((txbuffer[i] == '\n') || (txbuffer[i] == '\r')) {
-                txbuffer[i] = 0;
+    FILE *fid = (filename != NULL) ?  fopen (filename, "r") : stdin;
+    int rc = 0;
+    if (fid == NULL) {
+        VERBOSE (morep, ERROR, PRINTF ("can't open file '%s' for reading\n", filename));
+        rc = -1;
+    } else {
+        while ((!feof (fid)) && (!ferror (fid))) {
+            int txmsglen = MOREP_PAYLOAD;
+            uint8_t txbuffer[MOREP_PAYLOAD + 1] = {0};
+            for (int i = 0; i < txmsglen; i++) {
+                if ((fread (txbuffer + i, 1, 1, fid) < 1) || (txbuffer[i] == '\n') || (txbuffer[i] == '\r')) {
+                    txbuffer[i] = '\0';
+                    txmsglen = i;
+                    break;
+                }
+            }
+            if (ferror (fid)) {
+                VERBOSE (morep, ERROR, PRINTF ("can't read data from file '%s'\n", filename));
+                rc = -1;
+                break;
+            }
+            if (*txbuffer == '\0') {
+                continue;
             }
-        } while ((txbuffer[i] != 0) && (++i < txmsglen));
-        if ((i == 0) && feof (fid)) {
-            continue;
-        }
-        txmsglen = i;
-        if ((txmsglen == 0) && (errno != 0)) {
-            VERBOSE (morep, ERROR, PRINTF ("can't read data from file '%s'\n", filename));
-            return -1;
-        }
 
-        int txseqnum = MOREP_Send (morep, txmsgtype, txbuffer, txmsglen);
-        VERBOSE (morep, INFO, PRINTF ("tx %d bytes (msgid %d) [%d]\n", txmsglen, txmsgtype, txseqnum));
-        VERBOSE (morep, TRACE, PRINTF ("tx msg: '%s'\n", txbuffer));
+            int txseqnum = MOREP_Send (morep, txmsgtype, txbuffer, txmsglen);
+            VERBOSE (morep, INFO, PRINTF ("tx %d bytes (msgid %d) [%d]\n", txmsglen, txmsgtype, txseqnum));
+            VERBOSE (morep, TRACE, PRINTF ("tx msg: '%s'\n", txbuffer));
 
-        uint8_t rxmsgtype = 0;
-        int rxmsglen = 0;
-        uint8_t rxbuffer[MOREP_PAYLOAD] = {0};
-        int rxseqnum = MOREP_Receive (morep, &rxmsgtype, rxbuffer, &rxmsglen);
-        VERBOSE (morep, INFO, PRINTF ("rx %d bytes (msgid %d) [%d]\n", rxmsglen, rxmsgtype, rxseqnum));
-        VERBOSE (morep, TRACE, PRINTF ("rx msg: '"); fwrite ((char *)rxbuffer, 1, rxmsglen, stdout); printf ("'\n"));
-    }
+            uint8_t rxmsgtype = 0;
+            int rxmsglen = 0;
+            uint8_t rxbuffer[MOREP_PAYLOAD] = {0};
+            int rxseqnum = MOREP_Receive (morep, &rxmsgtype, rxbuffer, &rxmsglen);
+            VERBOSE (morep, INFO, PRINTF ("rx %d bytes (msgid %d) [%d]\n", rxmsglen, rxmsgtype, rxseqnum));
+            VERBOSE (morep, TRACE, PRINTF ("rx msg: '"); fwrite ((char *)rxbuffer, 1, rxmsglen, stdout); printf ("'\n"));
+        }
 
-    if (fid != stdin) {
-        fclose (fid);
+        if (fid != stdin) {
+            fclose (fid);
+        }
     }
     MOREP_Close (morep);
 
-    return 0;
+    return rc;
 }
 
 
@@ -166,6 +164,10 @@ char *readstream (FILE *sd, int *plen)
         buffer = (char *) realloc (buffer, size);
         memset (buffer + size - BUFMAX - 1, 0, BUFMAX + 1);
         blocklen = fread (buffer + size - BUFMAX - 1, 1, BUFMAX, sd);
+        if (ferror (sd)) {
+            VERBOSE (morep, ERROR, PRINTF ("can't read file\n"));
+            break;
+        }
         length += blocklen;
         VERBOSE (morep, DEBUG, PRINTF ("buffer: 0x%p 0x%p %d %d %d\n", buffer, buffer + size - BUFMAX - 1, (int)size, length, blocklen));
     } while (blocklen > 0);
index c982649d5e2f980d57aa1a142fcf6de885bdd862..40155d8addfa28196ae357d0b644594798101ff4 100644 (file)
@@ -86,10 +86,10 @@ char *read_stream (FILE *sd, int *plen)
 
 void print_message (FILE *fd, char *etype, int mode, uint8_t msg, int seqnum, uint8_t *payload, int len)
 {
-    fprintf (fd ? fd : stdout, "%c%s SEG=%d MSG=%d LEN=%d", mode ? 'T' : 'R', etype, seqnum, msg, len);
+    fprintf (fd, "%c%s SEG=%d MSG=%d LEN=%d", mode ? 'T' : 'R', etype, seqnum, msg, len);
     if (len > 0) {
         int i;
-        fprintf (fd ? fd : stdout, " PAYLOAD=");
+        fprintf (fd, " PAYLOAD=");
         for (i = 0; i < len; i++) {
             fprintf (fd, "%02x%c", payload[i], (i == len - 1) ? '\n' : ':');
         }
@@ -341,7 +341,7 @@ int main (int argc, char **argv)
     while (nbcomms) {
         MOREP_Close (--nbcomms);
     }
-    if ((log) && (log != stdout)) {
+    if (log) {
         fclose (log);
     }
 
diff --git a/parse.c b/parse.c
index 5129d9197594bf46e21610413a8e9c67e183483e..0ed3b871cb0f0cb2605f448b5db5713c2ff016fa 100644 (file)
--- a/parse.c
+++ b/parse.c
@@ -66,10 +66,12 @@ int parse_array (char *str, uint8_t *buffer, int maxlen)
         VERBOSE (morep, TRACE, PRINTF ("file payload: @filename\n"));
         FILE *fid = fopen (str + 1, "r");
         if (fid != NULL) {
-            while ((len < maxlen) && (!feof (fid))) {
+            while ((len < maxlen) && (!feof (fid)) && (!ferror (fid))) {
                 len += fread (buffer + len, 1, maxlen - len, fid);
             }
-            if (!feof (fid)) {
+            if (ferror (fid)) {
+                VERBOSE (morep, ERROR, PRINTF ("can't read file '%s'\n", str));
+            } else if (!feof (fid)) {
                 fseek (fid, 0L, SEEK_SET);
                 fseek (fid, 0L, SEEK_END);
                 int flen = ftell (fid);