nicob87 commented on a change in pull request #684: WIP: (Work In Progress)
DISPATCH-1568: using doctest
URL: https://github.com/apache/qpid-dispatch/pull/684#discussion_r381033409
##########
File path: src/router_core/terminus.c
##########
@@ -75,25 +76,47 @@ void qdr_terminus_free(qdr_terminus_t *term)
free_qdr_terminus_t(term);
}
+#ifdef TESTING
+__attribute__((weak))
+int mocked_vsnprintf(char *str, size_t size, const char *format, ...){return
-1;}
+#endif
// DISPATCH-1461: snprintf() is evil - it returns >= size on overflow. This
// wrapper will never return >= size, even if truncated. This makes it safe to
// do pointer & length arithmetic without overflowing the destination buffer in
// qdr_terminus_format()
-//
-static inline int safe_snprintf(char *str, size_t size, const char *format,
...)
-{
+// not static to be used unit-tested
+size_t safe_snprintf(char *str, size_t size, const char *format, ...) {
+ // max size allowed must be INT_MAX (since vsnprintf reutrns an int)
+ if (size == 0 || size > INT_MAX) {
+ //TODO log a warning somewhere?
+ return 0;
+ }
+ int max_possible_return_value = (int)(size - 1);
va_list ap;
va_start(ap, format);
+#ifdef TESTING
+ int rc = mocked_vsnprintf(str, size, format, ap);
Review comment:
Hi @jdanekrh, Awesome review! Thanks.
About the preprocessor, yes, I agree that code in this pr is polluted and it
is possible to do it cleaner, with a preprocessor "seam". More over that I do
not like using preprocessor at all. And also I do not like to compile twice
just to be able to use the preprocessor. BUT I was looking for a way to do not
pollute the production "binary". I didn't know a couple of tricks explained in
your documentation.
I think it is possible to do something like what is explained in [2]
(Run-time function interception) with the addition here we are mixing c and
c++. It looks like it may work :-P.
Will give it a try tomorrow or one of this days.
Regards!
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]