Ticket #2996: 0005-Use-snprintf-insted-of-sprintf_s.patch

File 0005-Use-snprintf-insted-of-sprintf_s.patch, 13.9 KB (added by BogDan, 9 years ago)
  • source/gui/IGUIObject.cpp

    From 5a19c50da319f8bd830fdf4b8477ee912cea9b31 Mon Sep 17 00:00:00 2001
    From: BogDan Vatra <bogdan@kde.org>
    Date: Sat, 10 Jan 2015 10:49:26 +0200
    Subject: [PATCH 05/11] Use snprintf insted of sprintf_s.
    
    There is no sprintf_s in the standard, but there is a snprintf which does the smae thing.
    ---
     source/gui/IGUIObject.cpp                          |  2 +-
     source/gui/scripting/ScriptFunctions.cpp           | 16 ++++++++--------
     source/network/NetServer.cpp                       |  2 +-
     source/ps/CLogger.cpp                              |  6 +++---
     source/ps/GUID.cpp                                 |  2 +-
     source/ps/Profile.cpp                              | 22 +++++++++++-----------
     source/ps/UserReport.cpp                           |  2 +-
     source/ps/XML/XeroXMB.cpp                          |  2 +-
     source/renderer/Renderer.cpp                       | 22 +++++++++++-----------
     .../simulation2/serialization/DebugSerializer.cpp  |  2 +-
     .../atlas/GameInterface/MessagePasserImpl.cpp      |  2 +-
     11 files changed, 40 insertions(+), 40 deletions(-)
    
    diff --git a/source/gui/IGUIObject.cpp b/source/gui/IGUIObject.cpp
    index d32cb28..54146f9 100644
    a b void IGUIObject::RegisterScriptHandler(const CStr& Action, const CStr& Code, CGU  
    433433    // Generate a unique name
    434434    static int x=0;
    435435    char buf[64];
    436     sprintf_s(buf, ARRAY_SIZE(buf), "__eventhandler%d (%s)", x++, Action.c_str());
     436    snprintf(buf, ARRAY_SIZE(buf), "__eventhandler%d (%s)", x++, Action.c_str());
    437437
    438438    JS::RootedFunction func(cx, JS_CompileFunction(cx, globalObj,
    439439        buf, paramCount, paramNames, Code.c_str(), Code.length(), CodeName.c_str(), 0));
  • source/gui/scripting/ScriptFunctions.cpp

    diff --git a/source/gui/scripting/ScriptFunctions.cpp b/source/gui/scripting/ScriptFunctions.cpp
    index 1f7f309..86863bd 100644
    a b std::wstring GetBuildTimestamp(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), i  
    792792        UDate dateTime = g_L10n.ParseDateTime(__DATE__ " " __TIME__, "MMM d yyyy HH:mm:ss", Locale::getUS());
    793793        std::string dateTimeString = g_L10n.LocalizeDateTime(dateTime, L10n::DateTime, SimpleDateFormat::DATE_TIME);
    794794        char svnRevision[32];
    795         sprintf_s(svnRevision, ARRAY_SIZE(svnRevision), "%ls", svn_revision);
     795        snprintf(svnRevision, ARRAY_SIZE(svnRevision), "%ls", svn_revision);
    796796        if (strcmp(svnRevision, "custom build") == 0)
    797797        {
    798798            // Translation: First item is a date and time, item between parenthesis is the Subversion revision number of the current build.
    799             sprintf_s(buf, ARRAY_SIZE(buf), g_L10n.Translate("%s (custom build)").c_str(), dateTimeString.c_str());
     799            snprintf(buf, ARRAY_SIZE(buf), g_L10n.Translate("%s (custom build)").c_str(), dateTimeString.c_str());
    800800        }
    801801        else
    802802        {
    803803            // Translation: First item is a date and time, item between parenthesis is the Subversion revision number of the current build.
    804             sprintf_s(buf, ARRAY_SIZE(buf), g_L10n.Translate("%s (%ls)").c_str(), dateTimeString.c_str(), svn_revision);
     804            snprintf(buf, ARRAY_SIZE(buf), g_L10n.Translate("%s (%ls)").c_str(), dateTimeString.c_str(), svn_revision);
    805805        }
    806806    }
    807807    else if (mode == 0) // Date.
    808808    {
    809809        UDate dateTime = g_L10n.ParseDateTime(__DATE__, "MMM d yyyy", Locale::getUS());
    810810        std::string dateTimeString = g_L10n.LocalizeDateTime(dateTime, L10n::Date, SimpleDateFormat::MEDIUM);
    811         sprintf_s(buf, ARRAY_SIZE(buf), "%s", dateTimeString.c_str());
     811        snprintf(buf, ARRAY_SIZE(buf), "%s", dateTimeString.c_str());
    812812    }
    813813    else if (mode == 1) // Time.
    814814    {
    815815        UDate dateTime = g_L10n.ParseDateTime(__TIME__, "HH:mm:ss", Locale::getUS());
    816816        std::string dateTimeString = g_L10n.LocalizeDateTime(dateTime, L10n::Time, SimpleDateFormat::MEDIUM);
    817         sprintf_s(buf, ARRAY_SIZE(buf), "%s", dateTimeString.c_str());
     817        snprintf(buf, ARRAY_SIZE(buf), "%s", dateTimeString.c_str());
    818818    }
    819819    else if (mode == 2) // Revision.
    820820    {
    821821        char svnRevision[32];
    822         sprintf_s(svnRevision, ARRAY_SIZE(svnRevision), "%ls", svn_revision);
     822        snprintf(svnRevision, ARRAY_SIZE(svnRevision), "%ls", svn_revision);
    823823        if (strcmp(svnRevision, "custom build") == 0)
    824824        {
    825             sprintf_s(buf, ARRAY_SIZE(buf), "%s", g_L10n.Translate("custom build").c_str());
     825            snprintf(buf, ARRAY_SIZE(buf), "%s", g_L10n.Translate("custom build").c_str());
    826826        }
    827827        else
    828828        {
    829             sprintf_s(buf, ARRAY_SIZE(buf), "%ls", svn_revision);
     829            snprintf(buf, ARRAY_SIZE(buf), "%ls", svn_revision);
    830830        }
    831831    }
    832832
  • source/network/NetServer.cpp

    diff --git a/source/network/NetServer.cpp b/source/network/NetServer.cpp
    index bcf904d..f119703 100644
    a b void* CNetServerWorker::SetupUPnP(void*)  
    205205{
    206206    // Values we want to set.
    207207    char psPort[6];
    208     sprintf_s(psPort, ARRAY_SIZE(psPort), "%d", PS_DEFAULT_PORT);
     208    snprintf(psPort, ARRAY_SIZE(psPort), "%d", PS_DEFAULT_PORT);
    209209    const char* leaseDuration = "0"; // Indefinite/permanent lease duration.
    210210    const char* description = "0AD Multiplayer";
    211211    const char* protocall = "UDP";
  • source/ps/CLogger.cpp

    diff --git a/source/ps/CLogger.cpp b/source/ps/CLogger.cpp
    index 1a68943..acfacbc 100644
    a b void CLogger::Init()  
    116116CLogger::~CLogger()
    117117{
    118118    char buffer[128];
    119     sprintf_s(buffer, ARRAY_SIZE(buffer), " with %d message(s), %d error(s) and %d warning(s).", m_NumberOfMessages,m_NumberOfErrors,m_NumberOfWarnings);
     119    snprintf(buffer, ARRAY_SIZE(buffer), " with %d message(s), %d error(s) and %d warning(s).", m_NumberOfMessages,m_NumberOfErrors,m_NumberOfWarnings);
    120120
    121121    time_t t = time(NULL);
    122122    struct tm* now = localtime(&t);
    123123    char currentDate[17];
    124     sprintf_s(currentDate, ARRAY_SIZE(currentDate), "%04d-%02d-%02d", 1900+now->tm_year, 1+now->tm_mon, now->tm_mday);
     124    snprintf(currentDate, ARRAY_SIZE(currentDate), "%04d-%02d-%02d", 1900+now->tm_year, 1+now->tm_mon, now->tm_mday);
    125125    char currentTime[10];
    126     sprintf_s(currentTime, ARRAY_SIZE(currentTime), "%02d:%02d:%02d", now->tm_hour, now->tm_min, now->tm_sec);
     126    snprintf(currentTime, ARRAY_SIZE(currentTime), "%02d:%02d:%02d", now->tm_hour, now->tm_min, now->tm_sec);
    127127
    128128    //Write closing text
    129129
  • source/ps/GUID.cpp

    diff --git a/source/ps/GUID.cpp b/source/ps/GUID.cpp
    index cf8807e..399d95a 100644
    a b CStr ps_generate_guid(void)  
    3333        u32 r = 0;
    3434        sys_generate_random_bytes((u8*)&r, sizeof(r));
    3535        char buf[32];
    36         sprintf_s(buf, ARRAY_SIZE(buf), "%08X", r);
     36        snprintf(buf, ARRAY_SIZE(buf), "%08X", r);
    3737        guid += buf;
    3838    }
    3939
  • source/ps/Profile.cpp

    diff --git a/source/ps/Profile.cpp b/source/ps/Profile.cpp
    index 7aa3f43..9823761 100644
    a b CStr CProfileNodeTable::GetName()  
    116116CStr CProfileNodeTable::GetTitle()
    117117{
    118118    char buf[512];
    119     sprintf_s(buf, ARRAY_SIZE(buf), "Profiling Information for: %s (Time in node: %.3f msec/frame)", node->GetName(), node->GetFrameTime() * 1000.0f );
     119    snprintf(buf, ARRAY_SIZE(buf), "Profiling Information for: %s (Time in node: %.3f msec/frame)", node->GetName(), node->GetFrameTime() * 1000.0f );
    120120    return buf;
    121121}
    122122
    CStr CProfileNodeTable::GetCellText(size_t row, size_t col)  
    186186        }
    187187
    188188        if (col == 2)
    189             sprintf_s(buf, ARRAY_SIZE(buf), "%.3f", unlogged_time_frame * 1000.0f);
     189            snprintf(buf, ARRAY_SIZE(buf), "%.3f", unlogged_time_frame * 1000.0f);
    190190        else if (col == 3)
    191             sprintf_s(buf, ARRAY_SIZE(buf), "%.1f", unlogged_mallocs_frame);
     191            snprintf(buf, ARRAY_SIZE(buf), "%.1f", unlogged_mallocs_frame);
    192192        else if (col == 5)
    193             sprintf_s(buf, ARRAY_SIZE(buf), "%.3f", unlogged_time_turn * 1000.f);
     193            snprintf(buf, ARRAY_SIZE(buf), "%.3f", unlogged_time_turn * 1000.f);
    194194        else if (col == 6)
    195             sprintf_s(buf, ARRAY_SIZE(buf), "%.1f", unlogged_mallocs_turn);
     195            snprintf(buf, ARRAY_SIZE(buf), "%.1f", unlogged_mallocs_turn);
    196196       
    197197        return CStr(buf);
    198198    }
    CStr CProfileNodeTable::GetCellText(size_t row, size_t col)  
    204204        return child->GetName();
    205205       
    206206    case 1:
    207         sprintf_s(buf, ARRAY_SIZE(buf), "%.1f", child->GetFrameCalls());
     207        snprintf(buf, ARRAY_SIZE(buf), "%.1f", child->GetFrameCalls());
    208208        break;
    209209    case 2:
    210         sprintf_s(buf, ARRAY_SIZE(buf), "%.3f", child->GetFrameTime() * 1000.0f);
     210        snprintf(buf, ARRAY_SIZE(buf), "%.3f", child->GetFrameTime() * 1000.0f);
    211211        break;
    212212    case 3:
    213         sprintf_s(buf, ARRAY_SIZE(buf), "%.1f", child->GetFrameMallocs());
     213        snprintf(buf, ARRAY_SIZE(buf), "%.1f", child->GetFrameMallocs());
    214214        break;
    215215    case 4:
    216         sprintf_s(buf, ARRAY_SIZE(buf), "%.1f", child->GetTurnCalls());
     216        snprintf(buf, ARRAY_SIZE(buf), "%.1f", child->GetTurnCalls());
    217217        break;
    218218    case 5:
    219         sprintf_s(buf, ARRAY_SIZE(buf), "%.3f", child->GetTurnTime() * 1000.0f);
     219        snprintf(buf, ARRAY_SIZE(buf), "%.3f", child->GetTurnTime() * 1000.0f);
    220220        break;
    221221    case 6:
    222         sprintf_s(buf, ARRAY_SIZE(buf), "%.1f", child->GetTurnMallocs());
     222        snprintf(buf, ARRAY_SIZE(buf), "%.1f", child->GetTurnMallocs());
    223223        break;
    224224    }
    225225    return CStr(buf);
  • source/ps/UserReport.cpp

    diff --git a/source/ps/UserReport.cpp b/source/ps/UserReport.cpp
    index 755542d..27d2b7b 100644
    a b std::string CUserReporter::LoadUserID()  
    533533        for (size_t i = 0; i < ARRAY_SIZE(bytes); ++i)
    534534        {
    535535            char hex[3];
    536             sprintf_s(hex, ARRAY_SIZE(hex), "%02x", (unsigned int)bytes[i]);
     536            snprintf(hex, ARRAY_SIZE(hex), "%02x", (unsigned int)bytes[i]);
    537537            userID += hex;
    538538        }
    539539
  • source/ps/XML/XeroXMB.cpp

    diff --git a/source/ps/XML/XeroXMB.cpp b/source/ps/XML/XeroXMB.cpp
    index 16ee3e7..35b4712 100644
    a b bool XMBFile::Initialise(const char* FileData)  
    3434{
    3535    m_Pointer = FileData;
    3636    char Header[5] = { 0 };
    37     strncpy_s(Header, 5, m_Pointer, 4);
     37    strncpy(Header, m_Pointer, 4);
    3838    m_Pointer += 4;
    3939    // (c.f. @return documentation of this function)
    4040    if(!strcmp(Header, UnfinishedHeaderMagicStr))
  • source/renderer/Renderer.cpp

    diff --git a/source/renderer/Renderer.cpp b/source/renderer/Renderer.cpp
    index 404d90a..0808792 100644
    a b CStr CRendererStatsTable::GetCellText(size_t row, size_t col)  
    169169    case Row_DrawCalls:
    170170        if (col == 0)
    171171            return "# draw calls";
    172         sprintf_s(buf, sizeof(buf), "%lu", (unsigned long)Stats.m_DrawCalls);
     172        snprintf(buf, sizeof(buf), "%lu", (unsigned long)Stats.m_DrawCalls);
    173173        return buf;
    174174
    175175    case Row_TerrainTris:
    176176        if (col == 0)
    177177            return "# terrain tris";
    178         sprintf_s(buf, sizeof(buf), "%lu", (unsigned long)Stats.m_TerrainTris);
     178        snprintf(buf, sizeof(buf), "%lu", (unsigned long)Stats.m_TerrainTris);
    179179        return buf;
    180180
    181181    case Row_WaterTris:
    182182        if (col == 0)
    183183            return "# water tris";
    184         sprintf_s(buf, sizeof(buf), "%lu", (unsigned long)Stats.m_WaterTris);
     184        snprintf(buf, sizeof(buf), "%lu", (unsigned long)Stats.m_WaterTris);
    185185        return buf;
    186186
    187187    case Row_ModelTris:
    188188        if (col == 0)
    189189            return "# model tris";
    190         sprintf_s(buf, sizeof(buf), "%lu", (unsigned long)Stats.m_ModelTris);
     190        snprintf(buf, sizeof(buf), "%lu", (unsigned long)Stats.m_ModelTris);
    191191        return buf;
    192192
    193193    case Row_OverlayTris:
    194194        if (col == 0)
    195195            return "# overlay tris";
    196         sprintf_s(buf, sizeof(buf), "%lu", (unsigned long)Stats.m_OverlayTris);
     196        snprintf(buf, sizeof(buf), "%lu", (unsigned long)Stats.m_OverlayTris);
    197197        return buf;
    198198
    199199    case Row_BlendSplats:
    200200        if (col == 0)
    201201            return "# blend splats";
    202         sprintf_s(buf, sizeof(buf), "%lu", (unsigned long)Stats.m_BlendSplats);
     202        snprintf(buf, sizeof(buf), "%lu", (unsigned long)Stats.m_BlendSplats);
    203203        return buf;
    204204
    205205    case Row_Particles:
    206206        if (col == 0)
    207207            return "# particles";
    208         sprintf_s(buf, sizeof(buf), "%lu", (unsigned long)Stats.m_Particles);
     208        snprintf(buf, sizeof(buf), "%lu", (unsigned long)Stats.m_Particles);
    209209        return buf;
    210210
    211211    case Row_VBReserved:
    212212        if (col == 0)
    213213            return "VB reserved";
    214         sprintf_s(buf, sizeof(buf), "%lu kB", (unsigned long)g_VBMan.GetBytesReserved() / 1024);
     214        snprintf(buf, sizeof(buf), "%lu kB", (unsigned long)g_VBMan.GetBytesReserved() / 1024);
    215215        return buf;
    216216
    217217    case Row_VBAllocated:
    218218        if (col == 0)
    219219            return "VB allocated";
    220         sprintf_s(buf, sizeof(buf), "%lu kB", (unsigned long)g_VBMan.GetBytesAllocated() / 1024);
     220        snprintf(buf, sizeof(buf), "%lu kB", (unsigned long)g_VBMan.GetBytesAllocated() / 1024);
    221221        return buf;
    222222
    223223    case Row_TextureMemory:
    224224        if (col == 0)
    225225            return "textures uploaded";
    226         sprintf_s(buf, sizeof(buf), "%lu kB", (unsigned long)g_Renderer.GetTextureManager().GetBytesUploaded() / 1024);
     226        snprintf(buf, sizeof(buf), "%lu kB", (unsigned long)g_Renderer.GetTextureManager().GetBytesUploaded() / 1024);
    227227        return buf;
    228228
    229229    case Row_ShadersLoaded:
    230230        if (col == 0)
    231231            return "shader effects loaded";
    232         sprintf_s(buf, sizeof(buf), "%lu", (unsigned long)g_Renderer.GetShaderManager().GetNumEffectsLoaded());
     232        snprintf(buf, sizeof(buf), "%lu", (unsigned long)g_Renderer.GetShaderManager().GetNumEffectsLoaded());
    233233        return buf;
    234234
    235235    default:
  • source/simulation2/serialization/DebugSerializer.cpp

    diff --git a/source/simulation2/serialization/DebugSerializer.cpp b/source/simulation2/serialization/DebugSerializer.cpp
    index 07cdd76..16de1ee 100644
    a b void CDebugSerializer::PutRaw(const char* name, const u8* data, size_t len)  
    161161    char buf[4];
    162162    for (size_t i = 0; i < len; ++i)
    163163    {
    164         sprintf_s(buf, ARRAY_SIZE(buf), " %02x", (unsigned int)data[i]);
     164        snprintf(buf, ARRAY_SIZE(buf), " %02x", (unsigned int)data[i]);
    165165        m_Stream << buf;
    166166    }
    167167
  • source/tools/atlas/GameInterface/MessagePasserImpl.cpp

    diff --git a/source/tools/atlas/GameInterface/MessagePasserImpl.cpp b/source/tools/atlas/GameInterface/MessagePasserImpl.cpp
    index 7f324a0..ae32eab 100644
    a b MessagePasserImpl::MessagePasserImpl()  
    3636    while (tries++ < 16) // some arbitrary cut-off point to avoid infinite loops
    3737    {
    3838        static char name[64];
    39         sprintf_s(name, ARRAY_SIZE(name), "/wfg-atlas-msgpass-%d-%d",
     39        snprintf(name, ARRAY_SIZE(name), "/wfg-atlas-msgpass-%d-%d",
    4040                (int)rand(1, 1000), (int)(time(0)%1000));
    4141        sem_t* sem = sem_open(name, O_CREAT | O_EXCL, 0700, 0);
    4242