]> git.ipfire.org Git - thirdparty/bacula.git/commitdiff
pluginlib: Implement scan_and_terminate_str.
authorRadosław Korzeniewski <radoslaw@korzeniewski.net>
Fri, 7 May 2021 15:07:20 +0000 (17:07 +0200)
committerEric Bollengier <eric@baculasystems.com>
Thu, 24 Mar 2022 08:03:02 +0000 (09:03 +0100)
bacula/src/plugins/fd/pluginlib/pluginlib.cpp
bacula/src/plugins/fd/pluginlib/pluginlib.h
bacula/src/plugins/fd/pluginlib/pluginlib_test.cpp
bacula/src/plugins/fd/pluginlib/ptcomm.cpp

index 8cb115a5e99ff34250b091ac476f15371f1ecd5e..377f249cb45bb11b4d0422747eae664e1b724025 100644 (file)
@@ -633,3 +633,15 @@ bool scan_parameter_str(const char * cmd, const char *prefix, POOL_MEM &param)
 
    return false;
 }
+
+// ensure error message is terminated with newline and terminated with standard c-string nul
+void scan_and_terminate_str(POOL_MEM &buf, int msglen)
+{
+   if (msglen >= 0){
+      // we will consume at most two chars more
+      buf.check_size(msglen + 2);
+      bool check = msglen > 0 ? buf.c_str()[msglen - 1] != '\n' : true;
+      buf.c_str()[msglen] = check * '\n';
+      buf.c_str()[msglen + 1] = '\0';
+   }
+}
index 254066092e4abd5b329629681ceb7b7b47efd427..8fd2a7a1dd443974e1a90bf5302634ac36a9d0ea 100644 (file)
@@ -223,5 +223,6 @@ bool parse_param_add_str(alist &list, const char *pname, const char *name, const
 bool scan_parameter_str(const char * cmd, const char *prefix, POOL_MEM &param);
 inline bool scan_parameter_str(const POOL_MEM &cmd, const char *prefix, POOL_MEM &param) { return scan_parameter_str(cmd.c_str(), prefix, param); }
 
-#endif   /* _PLUGINLIB_H_ */
+void scan_and_terminate_str(POOL_MEM &buf, int msglen);
 
+#endif /* _PLUGINLIB_H_ */
index 3cad82df804da04cfca8eedf792a1f9b833df807..83ab66bfc081cd896807d5e90b633eb843b7cf38 100644 (file)
@@ -173,5 +173,32 @@ int main()
       ok(result == testvect1[i].result, testvect1[i].descr);
    }
 
+   struct strvectstruct
+   {
+      const char *input;
+      const char *output;
+      const int msglen;
+      const int len;
+      const char *descr;
+   };
+   const strvectstruct testvect2[] = {
+      { "TEST1****", "TEST1\n\0", 5, 7, "TEST1" },
+      { "TEST2\n****", "TEST2\n\0", 6, 7, "TEST2" },
+      { "TEST3\n\0****", "TEST3\n\0", 7, 7, "TEST3" },
+      { "TEST4\0****", "TEST4\n\0", 6, 7, "TEST4" },
+      { "TEST5\0\n****", "TEST5\n\0", 7, 7, "TEST5" },
+      { "****", "\n\0", 0, 2, "Test empty" },
+      { NULL, NULL, 0, 0, NULL },
+   };
+
+   POOL_MEM ebuf(PM_NAME);
+   for (int i = 0; testvect2[i].input != NULL; i++)
+   {
+      pm_memcpy(ebuf, testvect2[i].input, strlen(testvect2[i].input));
+      scan_and_terminate_str(ebuf, testvect2[i].msglen);
+      ok(memcmp(ebuf.c_str(), testvect2[i].output, testvect2[i].len) == 0, testvect2[i].descr);
+   }
+   // scan_and_terminate_str
+
    return report();
 }
index 95011138d00b391a2b9a3769235ca7281ee8b43b..f8bfb1ec749d896ca82d949dca8fec424ba8d492 100644 (file)
@@ -353,10 +353,8 @@ int32_t PTCOMM::recvbackend_header(bpContext *ctx, char cmd)
          return -1;
       }
 
-      // ensure error message is terminated with newline and
-      // terminated with standard c-string nul
-      errmsg.c_str()[msglen] = errmsg.c_str()[msglen - 1] != '\n' ? '\n' : '\0';
-      errmsg.c_str()[msglen + 1] = '\0';
+      // ensure error message is terminated with newline and terminated with standard c-string nul
+      scan_and_terminate_str(errmsg, msglen);
 
       switch (header.status)
       {