parlib: Fix "can't print 'ret'" bug
authorBarret Rhoden <brho@cs.berkeley.edu>
Fri, 18 Aug 2017 20:21:44 +0000 (16:21 -0400)
committerBarret Rhoden <brho@cs.berkeley.edu>
Tue, 22 Aug 2017 20:14:49 +0000 (16:14 -0400)
Thanks to nasty macros, if you tried to do something like:

int ret = 3;
printf("Return value is %d\n", ret);

You'd get "Return value is 0".  The problem was that the 'ret' variable
inside the macro would be the one printed, not the one passed in.

Signed-off-by: Barret Rhoden <brho@cs.berkeley.edu>
user/parlib/include/parlib/stdio.h

index ac5e517..4b9467f 100644 (file)
@@ -75,35 +75,36 @@ static inline bool __safe_to_printf(void)
 
 #define vfprintf(f_stream, fmt, ...)                                           \
 ({                                                                             \
-       int ret;                                                                   \
+       int __parlib_safe_print_ret;                                               \
                                                                                   \
        if (__safe_to_printf())                                                    \
-               ret = vfprintf(f_stream, fmt, __VA_ARGS__);                            \
+               __parlib_safe_print_ret = vfprintf(f_stream, fmt, __VA_ARGS__);        \
        else                                                                       \
-               ret = __vc_ctx_vfprintf(f_stream, fmt, __VA_ARGS__);                   \
-       ret;                                                                       \
+               __parlib_safe_print_ret = __vc_ctx_vfprintf(f_stream, fmt,             \
+                                                           __VA_ARGS__);              \
+       __parlib_safe_print_ret;                                                   \
 })
 
 #define fprintf(f_stream, ...)                                                 \
 ({                                                                             \
-       int ret;                                                                   \
+       int __parlib_safe_print_ret;                                               \
                                                                                   \
        if (__safe_to_printf())                                                    \
-               ret = fprintf(f_stream, __VA_ARGS__);                                  \
+               __parlib_safe_print_ret = fprintf(f_stream, __VA_ARGS__);              \
        else                                                                       \
-               ret = __vc_ctx_fprintf(f_stream, __VA_ARGS__);                         \
-       ret;                                                                       \
+               __parlib_safe_print_ret = __vc_ctx_fprintf(f_stream, __VA_ARGS__);     \
+       __parlib_safe_print_ret;                                                   \
 })
 
 #define printf(...)                                                            \
 ({                                                                             \
-       int ret;                                                                   \
+       int __parlib_safe_print_ret;                                               \
                                                                                   \
        if (__safe_to_printf())                                                    \
-               ret = printf(__VA_ARGS__);                                             \
+               __parlib_safe_print_ret = printf(__VA_ARGS__);                         \
        else                                                                       \
-               ret = __vc_ctx_fprintf(stdout, __VA_ARGS__);                           \
-       ret;                                                                       \
+               __parlib_safe_print_ret = __vc_ctx_fprintf(stdout, __VA_ARGS__);       \
+       __parlib_safe_print_ret;                                                   \
 })
 
 #define I_AM_HERE __vc_ctx_fprintf(stderr,                                     \