]> git.ipfire.org Git - thirdparty/bacula.git/commitdiff
docker: Fix variable not initialized in the constructor.
authorRadosław Korzeniewski <radoslaw@korzeniewski.net>
Tue, 17 Nov 2020 14:11:02 +0000 (15:11 +0100)
committerEric Bollengier <eric@baculasystems.com>
Thu, 24 Mar 2022 08:02:59 +0000 (09:02 +0100)
bacula/src/plugins/fd/docker/dkid.c
bacula/src/plugins/fd/docker/dkid.h

index 7813d9548fd1294aa5aac04ec39a51e3f7bd29e4..3430aeb52839f0c0f85f723cd00dcc767928da8b 100644 (file)
 /*
  * DKID class constructor, does default initialization.
  */
-DKID::DKID()
-{
-   bmemzero(Digest, DKIDDIGESTSIZE + 1);
-   ShortD = DKIDInvalid;
-   shortonly = false;
-};
-
-/*
- * DKID class constructor, does parm initialization.
- */
-DKID::DKID(const char* data)
-{
-   init(data);
-};
-
-/*
- * DKID class constructor, does parm initialization.
- */
-DKID::DKID(POOL_MEM& data)
+DKID::DKID() :
+   ShortD(DKIDInvalid),
+#if __cplusplus > 201103L
+   Digest{0},
+   DigestShort{0},
+#endif
+   shortonly(false)
 {
-   init(data.c_str());
+#if ! __cplusplus > 201103L
+   bmemzero(Digest, DKIDDIGESTSIZE_Len);
+   bmemzero(DigestShort, DKIDDIGESTShortSIZE_Len);
+#endif
 };
 
 /*
@@ -60,10 +51,6 @@ DKID::DKID(POOL_MEM& data)
  */
 void DKID::init(const char* data)
 {
-   int len;
-   int a;
-   unsigned char c;
-   bool valid = true;
    char *dig = (char*)data;
 
    if (dig != NULL){
@@ -71,11 +58,12 @@ void DKID::init(const char* data)
       if (strstr(dig, "sha256:") == dig){
          dig += 7;
       }
-      len = strlen(dig);
+      int len = strlen(dig);
       /* check for invalid input data */
-      for (a = 0; a < (len > DKIDDIGESTShortSIZE ? DKIDDIGESTShortSIZE : len); a++){
+      bool valid = true;
+      for (int a = 0; a < (len > DKIDDIGESTShortSIZE ? DKIDDIGESTShortSIZE : len); a++){
          // we are checking for ASCII codes, a subset of UTF-8 for short digest only
-         c = (unsigned char)dig[a];
+         unsigned char c = (unsigned char)dig[a];
          if (c > 'f' || (c > '9' && c < 'A') || (c > 'F' && c < 'a')){
             valid = false;
             break;
@@ -350,4 +338,4 @@ int main()
    return report();
 };
 
-#endif   /* TEST_PROGRAM */
\ No newline at end of file
+#endif   /* TEST_PROGRAM */
index cca496f5b1f27aa5be30ef01ffab045c5a4a730c..47e1ad5de80bce50afed083e030f482880427c97 100644 (file)
 
 #include "bacula.h"
 
-#define DKIDDIGESTSIZE        64       // the size of string array for hex chars, without trailing nul
-#define DKIDDIGESTShortSIZE   12       // the number of hex characters in short digest, without trailing nul
-#define DKIDInvalid           -256     // the non-trivial negative value :)
+#define DKIDDIGESTSIZE           64                         // the size of string array for hex chars, without trailing nul
+#define DKIDDIGESTSIZE_Len       (DKIDDIGESTSIZE + 1)       // the size of string array for hex chars, with trailing nul
+#define DKIDDIGESTShortSIZE      12                         // the number of hex characters in short digest, without trailing nul
+#define DKIDDIGESTShortSIZE_Len  (DKIDDIGESTShortSIZE + 1)  // the number of hex characters in short digest, without trailing nul
+#define DKIDInvalid              -256                       // the non-trivial negative value :)
 
 /*
  * This is a simple storage class to handle Docker Container IDs
  */
-class DKID: public SMARTALLOC {
- public:
-    DKID();
-    DKID(const char *data);
-    DKID(POOL_MEM &data);
-    ~DKID() {};
+class DKID: public SMARTALLOC
+{
+public:
+   DKID();
+   DKID(const char *data) { init(data); }
+   DKID(POOL_MEM &data) { init(data.c_str()); }
+#if __cplusplus > 201103L
+   ~DKID() = default;
+#else
+   ~DKID() {};
+#endif
 
-    inline int64_t id() { return ShortD; };
-    inline char *digest() { return Digest; };
-    inline char *digest_short() { return DigestShort; };
-    inline operator int64_t () { return ShortD; };
-    inline operator char* () { return Digest; };
-    DKID& operator= (char *data);
-    DKID& operator= (DKID &other);
-    DKID& operator= (POOL_MEM &data);
-    bool operator== (DKID &other);
-    bool operator!= (DKID &other);
+   inline int64_t id() { return ShortD; };
+   inline char *digest() { return Digest; };
+   inline char *digest_short() { return DigestShort; };
+   inline operator int64_t () { return ShortD; };
+   inline operator char* () { return Digest; };
+   DKID& operator= (char *data);
+   DKID& operator= (DKID &other);
+   DKID& operator= (POOL_MEM &data);
+   bool operator== (DKID &other);
+   bool operator!= (DKID &other);
 #ifdef TEST_PROGRAM
-    void dump();
+   void dump();
 #endif
 
- private:
-   char Digest[DKIDDIGESTSIZE + 1];
-   char DigestShort[DKIDDIGESTShortSIZE + 1];
+private:
    int64_t ShortD; // default short digest on Docker is 48bits/6bytes/12hex chars
                    // https://github.com/docker/cli/blob/master/vendor/github.com/docker/docker/pkg/stringid/stringid.go
+   char Digest[DKIDDIGESTSIZE_Len];
+   char DigestShort[DKIDDIGESTShortSIZE_Len];
    bool shortonly;
 
    void init(const char* d);