]> arthur.barton.de Git - netdata.git/blobdiff - src/procfile.c
improvements identified via static code analysis with cppcheck
[netdata.git] / src / procfile.c
index e2aa605820e4197dc1d9cb1809bcced5f53f21ed..31bdb5e25786aa01a57e0c33ecd407d4d5328bb4 100644 (file)
@@ -267,14 +267,14 @@ cleanup:
 procfile *procfile_readall(procfile *ff) {
     debug(D_PROCFILE, PF_PREFIX ": Reading file '%s'.", ff->filename);
 
-    ssize_t s, r = 1, x;
+    ssize_t r = 1;
     ff->len = 0;
 
     while(likely(r > 0)) {
-        s = ff->len;
-        x = ff->size - s;
+        ssize_t s = ff->len;
+        ssize_t x = ff->size - s;
 
-        if(!x) {
+        if(unlikely(!x)) {
             debug(D_PROCFILE, PF_PREFIX ": Expanding data buffer for file '%s'.", ff->filename);
 
             ff = reallocz(ff, sizeof(procfile) + ff->size + PROCFILE_INCREMENT_BUFFER);
@@ -316,13 +316,13 @@ procfile *procfile_readall(procfile *ff) {
 
 static void procfile_set_separators(procfile *ff, const char *separators) {
     static char def[256] = { [0 ... 255] = 0 };
-    int i;
 
     if(unlikely(!def[255])) {
         // this is thread safe
         // we check that the last byte is non-zero
         // if it is zero, multiple threads may be executing this at the same time
         // setting in def[] the exact same values
+        int i;
         for(i = 0; likely(i < 256) ;i++) {
             if(unlikely(i == '\n' || i == '\r')) def[i] = PF_CHAR_IS_NEWLINE;
             else if(unlikely(isspace(i) || !isprint(i))) def[i] = PF_CHAR_IS_SEPARATOR;
@@ -348,7 +348,7 @@ void procfile_set_quotes(procfile *ff, const char *quotes) {
     // remove all quotes
     int i;
     for(i = 0; i < 256 ; i++)
-        if(ff->separators[i] == PF_CHAR_IS_QUOTE)
+        if(unlikely(ff->separators[i] == PF_CHAR_IS_QUOTE))
             ff->separators[i] = PF_CHAR_IS_WORD;
 
     // if nothing given, return
@@ -366,7 +366,7 @@ void procfile_set_open_close(procfile *ff, const char *open, const char *close)
     // remove all open/close
     int i;
     for(i = 0; i < 256 ; i++)
-        if(ff->separators[i] == PF_CHAR_IS_OPEN || ff->separators[i] == PF_CHAR_IS_CLOSE)
+        if(unlikely(ff->separators[i] == PF_CHAR_IS_OPEN || ff->separators[i] == PF_CHAR_IS_CLOSE))
             ff->separators[i] = PF_CHAR_IS_WORD;
 
     // if nothing given, return
@@ -443,16 +443,16 @@ procfile *procfile_reopen(procfile *ff, const char *filename, const char *separa
 
 void procfile_print(procfile *ff) {
     uint32_t lines = procfile_lines(ff), l;
-    uint32_t words, w;
     char *s;
 
     debug(D_PROCFILE, "File '%s' with %u lines and %u words", ff->filename, ff->lines->len, ff->words->len);
 
     for(l = 0; likely(l < lines) ;l++) {
-        words = procfile_linewords(ff, l);
+        uint32_t words = procfile_linewords(ff, l);
 
         debug(D_PROCFILE, " line %u starts at word %u and has %u words", l, ff->lines->lines[l].first, ff->lines->lines[l].words);
 
+        uint32_t w;
         for(w = 0; likely(w < words) ;w++) {
             s = procfile_lineword(ff, l, w);
             debug(D_PROCFILE, "     [%u.%u] '%s'", l, w, s);