From: RadosÅ‚aw Korzeniewski Date: Fri, 7 May 2021 15:07:20 +0000 (+0200) Subject: pluginlib: Implement scan_and_terminate_str. X-Git-Tag: Release-11.3.2~519 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a5b08367c52c7e36836ddaea359e6ba3d2a18737;p=thirdparty%2Fbacula.git pluginlib: Implement scan_and_terminate_str. --- diff --git a/bacula/src/plugins/fd/pluginlib/pluginlib.cpp b/bacula/src/plugins/fd/pluginlib/pluginlib.cpp index 8cb115a5e..377f249cb 100644 --- a/bacula/src/plugins/fd/pluginlib/pluginlib.cpp +++ b/bacula/src/plugins/fd/pluginlib/pluginlib.cpp @@ -633,3 +633,15 @@ bool scan_parameter_str(const char * cmd, const char *prefix, POOL_MEM ¶m) 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'; + } +} diff --git a/bacula/src/plugins/fd/pluginlib/pluginlib.h b/bacula/src/plugins/fd/pluginlib/pluginlib.h index 254066092..8fd2a7a1d 100644 --- a/bacula/src/plugins/fd/pluginlib/pluginlib.h +++ b/bacula/src/plugins/fd/pluginlib/pluginlib.h @@ -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 ¶m); inline bool scan_parameter_str(const POOL_MEM &cmd, const char *prefix, POOL_MEM ¶m) { 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_ */ diff --git a/bacula/src/plugins/fd/pluginlib/pluginlib_test.cpp b/bacula/src/plugins/fd/pluginlib/pluginlib_test.cpp index 3cad82df8..83ab66bfc 100644 --- a/bacula/src/plugins/fd/pluginlib/pluginlib_test.cpp +++ b/bacula/src/plugins/fd/pluginlib/pluginlib_test.cpp @@ -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(); } diff --git a/bacula/src/plugins/fd/pluginlib/ptcomm.cpp b/bacula/src/plugins/fd/pluginlib/ptcomm.cpp index 95011138d..f8bfb1ec7 100644 --- a/bacula/src/plugins/fd/pluginlib/ptcomm.cpp +++ b/bacula/src/plugins/fd/pluginlib/ptcomm.cpp @@ -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) {