From 7a12d832394176890ae9cbe8c869c655b290eac1 Mon Sep 17 00:00:00 2001 From: Adeel Kazmi Date: Tue, 10 Mar 2020 14:36:32 +0000 Subject: [PATCH] (Deferred Shading) Fix some SVACE errors 1) Should use snprintf instead of sprintf 2) Should use strncpy instead of strcpy Change-Id: If13366f0f176fdad3948bc1e8e425e430c233fc6 --- examples/deferred-shading/deferred-shading.cpp | 30 +++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/examples/deferred-shading/deferred-shading.cpp b/examples/deferred-shading/deferred-shading.cpp index 425e533..72ca6c5 100644 --- a/examples/deferred-shading/deferred-shading.cpp +++ b/examples/deferred-shading/deferred-shading.cpp @@ -458,6 +458,26 @@ void RegisterDepthProperties(float depth, float near, Handle& h) } //============================================================================= +/// Create a String whose size can be evaluated at compile time +struct ConstantString +{ + const char * const string; + const uint16_t size; + + template + constexpr ConstantString(const char (&input)[inputSize]) + : string(input), + size(inputSize) + { + } +}; + +constexpr ConstantString POSITION_STRING("position"); +constexpr ConstantString RADIUS_STRING("radius"); +constexpr ConstantString COLOR_STRING("color"); +constexpr uint16_t LIGHT_SOURCE_BUFFER_SIZE(128u); + +//============================================================================= class DeferredShadingExample : public ConnectionTracker { public: @@ -711,17 +731,17 @@ private: auto iPropLightColor = light.RegisterProperty("lightcolor", color); // Create light source uniforms on lighting shader. - char buffer[128]; - char* writep = buffer + sprintf(buffer, "uLights[%d].", mNumLights); + char buffer[LIGHT_SOURCE_BUFFER_SIZE]; + char* writep = buffer + snprintf(buffer, LIGHT_SOURCE_BUFFER_SIZE, "uLights[%d].", mNumLights); ++mNumLights; - strcpy(writep, "position"); + strncpy(writep, POSITION_STRING.string, POSITION_STRING.size); auto oPropLightPos = renderer.RegisterProperty(buffer, position); - strcpy(writep, "radius"); + strncpy(writep, RADIUS_STRING.string, RADIUS_STRING.size); auto oPropLightRadius = renderer.RegisterProperty(buffer, radius); - strcpy(writep, "color"); + strncpy(writep, COLOR_STRING.string, COLOR_STRING.size); auto oPropLightColor = renderer.RegisterProperty(buffer, color); // Constrain the light position, radius and color to lighting shader uniforms. -- 2.7.4