[vlc-commits] [Git][videolan/libvlcpp][master] Instance: avoid using asprintf

Steve Lhomme (@robUx4) gitlab at videolan.org
Thu Mar 30 09:24:12 UTC 2023



Steve Lhomme pushed to branch master at VideoLAN / libvlcpp


Commits:
d76fe067 by Steve Lhomme at 2023-03-27T09:30:24+02:00
Instance: avoid using asprintf

This is a GNU function. We have the tool in C++ to concatenate strings
and integers.

Fixes #14

- - - - -


1 changed file:

- vlcpp/Instance.hpp


Changes:

=====================================
vlcpp/Instance.hpp
=====================================
@@ -33,6 +33,7 @@
 
 #include <algorithm>
 #include <string>
+#include <sstream>
 #include <vector>
 #include <cstring>
 #include <cstdio>
@@ -261,21 +262,17 @@ public:
             char* psz_msg = message.get();
             if (vsnprintf(psz_msg, len + 1, format, va) < 0 )
                 return;
-            char* psz_ctx;
-            if (asprintf(&psz_ctx, "[%s] (%s:%d) %s", psz_module, psz_file, i_line, psz_msg) < 0)
-                return;
-            std::unique_ptr<char, void(*)(void*)> ctxPtr(psz_ctx, &free);
 #else
             //MSVC treats passing nullptr as 1st vsnprintf(_s) as an error
             char psz_msg[512];
             if ( _vsnprintf_s( psz_msg, _TRUNCATE, format, va ) < 0 )
                 return;
-            char psz_ctx[1024];
-            if( sprintf_s( psz_ctx, "[%s] (%s:%d) %s", psz_module, psz_file,
-                          i_line, psz_msg ) < 0 )
-                return;
 #endif
-            logCb( level, ctx, std::string{ psz_ctx } );
+            std::ostringstream ss;
+            ss << '[' << psz_module << "] ("
+               << psz_file << ':' << i_line
+               << ") " << psz_msg;
+            logCb( level, ctx, ss.str() );
         };
         libvlc_log_set(*this, CallbackWrapper<(unsigned int)CallbackIdx::Log, libvlc_log_cb>::wrap( *m_callbacks, std::move(wrapper)),
             m_callbacks.get() );



View it on GitLab: https://code.videolan.org/videolan/libvlcpp/-/commit/d76fe0678e92ada5897eac975e14edc9981130e8

-- 
View it on GitLab: https://code.videolan.org/videolan/libvlcpp/-/commit/d76fe0678e92ada5897eac975e14edc9981130e8
You're receiving this email because of your account on code.videolan.org.


VideoLAN code repository instance


More information about the vlc-commits mailing list