jdanekrh 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_r380620641
 
 

 ##########
 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:
   I was thinking about this and I don't like this much. I understand that in 
C, there isn't many options to replace vsnprintf calls in tests, but this 
"pollutes" the production code a lot.
   
   In the C++ world, they seem to call these techniques "seams", [1] [2] [3]. 
Dispatch already uses the linker seam in two places, to mock 
`qd_server_timeout` and `qd_timer_now`.
   
   I generally like using the linker better, it seems cleaner to me.
   
   [1] http://www.informit.com/articles/article.aspx?p=359417&seqNum=3
   [2] https://accu.org/index.php/journals/1927 Refactoring Towards Seams in C++
   [3] https://cute-test.com/guides/mocking-with-cute/

----------------------------------------------------------------
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:
us...@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@qpid.apache.org
For additional commands, e-mail: dev-h...@qpid.apache.org

Reply via email to