Ticket #2996: 0006-1-Fix-crappy-vswprintf-implemenation-on-Android.2.patch

File 0006-1-Fix-crappy-vswprintf-implemenation-on-Android.2.patch, 1.7 KB (added by BogDan, 9 years ago)
  • source/lib/secure_crt.cpp

    From 2cc956ddb59c1e12a0a1aac481d2a99d6437d9d6 Mon Sep 17 00:00:00 2001
    From: BogDan Vatra <bogdan@kde.org>
    Date: Wed, 14 Jan 2015 09:46:59 +0200
    Subject: [PATCH] Fix crappy vswprintf implemenation on Android.
    
    It was the main reason for most of the crashes (e.g. sound).
    ---
     source/lib/secure_crt.cpp | 21 +++++++++++++++++++++
     1 file changed, 21 insertions(+)
    
    diff --git a/source/lib/secure_crt.cpp b/source/lib/secure_crt.cpp
    index 849c76e..b412758 100644
    a b  
    3232
    3333#include "lib/secure_crt.h"
    3434
     35#if OS_ANDROID
     36# include <boost/algorithm/string/replace.hpp>
     37#endif
     38
    3539// we were included from wsecure_crt.cpp; skip all stuff that
    3640// must only be done once.
    3741#ifndef WSECURE_CRT
    size_t tnlen(const tchar* str, size_t max_len)  
    127131}
    128132#endif // !OS_UNIX
    129133
     134#if OS_ANDROID
     135static std::wstring androidFormat(const tchar *fmt)
     136{
     137// FIXME handle %%hs, %%ls
     138    std::wstring ret(fmt);
     139    boost::algorithm::replace_all(ret, L"%ls", L"%S");
     140    boost::algorithm::replace_all(ret, L"%.*ls", L"%.*S");
     141    boost::algorithm::replace_all(ret, L"%hs", L"%s");
     142    boost::algorithm::replace_all(ret, L"%.*hs", L"%.*s");
     143    return std::move(ret);
     144}
     145#endif
    130146
    131147// copy at most <max_src_chars> (not including trailing null) from
    132148// <src> into <dst>, which must not overlap.
    int tvsprintf_s(tchar* dst, size_t max_dst_chars, const tchar* fmt, va_list ap)  
    236252        return -1;
    237253    }
    238254
     255#if OS_ANDROID
     256    memset(dst, 0, max_dst_chars * sizeof(tchar));
     257    const int ret = tvsnprintf(dst, max_dst_chars, androidFormat(fmt).c_str(), ap);
     258#else
    239259    const int ret = tvsnprintf(dst, max_dst_chars, fmt, ap);
     260#endif
    240261    if(ret >= int(max_dst_chars))   // not enough space
    241262    {
    242263        dst[0] = '\0';