From db8a222f629b56394f36bf102d1f9d34a019e62e Mon Sep 17 00:00:00 2001 From: Gawaboumga Date: Tue, 31 May 2016 21:50:31 +0200 Subject: [PATCH] Fix for problems signaled by clang static analyzer Former-commit-id: 835f639c45509b0d55fe716e51e3285ea2a89da4 --- include/Nazara/Network/RUdpConnection.inl | 2 +- src/Nazara/Core/Posix/FileImpl.cpp | 13 +++++++- src/Nazara/Core/String.cpp | 31 ++++++-------------- src/Nazara/Graphics/DepthRenderTechnique.cpp | 3 +- tests/Engine/Core/String.cpp | 20 +++++++++++++ 5 files changed, 43 insertions(+), 26 deletions(-) diff --git a/include/Nazara/Network/RUdpConnection.inl b/include/Nazara/Network/RUdpConnection.inl index 53e797b59..48173bd77 100644 --- a/include/Nazara/Network/RUdpConnection.inl +++ b/include/Nazara/Network/RUdpConnection.inl @@ -132,7 +132,7 @@ namespace Nz else difference = sequence - sequence2; - return 0; + return difference; } /*! diff --git a/src/Nazara/Core/Posix/FileImpl.cpp b/src/Nazara/Core/Posix/FileImpl.cpp index f6e8b3bcc..b9a6b0a31 100644 --- a/src/Nazara/Core/Posix/FileImpl.cpp +++ b/src/Nazara/Core/Posix/FileImpl.cpp @@ -143,7 +143,18 @@ namespace Nz return false; } - mode_t permissions; // TODO : get permission from first file + mode_t permissions; + struct stat sb; + if (fstat(fd1, &sb) == -1) // get permission from first file + { + NazaraWarning("Could not get permissions of source file"); + permissions = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; + } + else + { + permissions = sb.st_mode & ~S_IFMT; // S_IFMT: bit mask for the file type bit field -> ~S_IFMT: general permissions + } + int fd2 = open64(targetPath.GetConstBuffer(), O_WRONLY | O_TRUNC, permissions); if (fd2 == -1) { diff --git a/src/Nazara/Core/String.cpp b/src/Nazara/Core/String.cpp index a57d89861..20d1c1897 100644 --- a/src/Nazara/Core/String.cpp +++ b/src/Nazara/Core/String.cpp @@ -2827,21 +2827,15 @@ namespace Nz { const char* c = oldCharacters; char character = Detail::ToLower(*ptr); - bool found = false; + + std::ptrdiff_t offset = ptr - m_sharedString->string.get(); + EnsureOwnership(); + ptr = &m_sharedString->string[offset]; + do { if (character == Detail::ToLower(*c)) { - if (!found) - { - std::ptrdiff_t offset = ptr - m_sharedString->string.get(); - - EnsureOwnership(); - - ptr = &m_sharedString->string[offset]; - found = true; - } - *ptr = replaceCharacter; ++count; break; @@ -2853,19 +2847,12 @@ namespace Nz } else { - bool found = false; + std::ptrdiff_t offset = ptr - m_sharedString->string.get(); + EnsureOwnership(); + ptr = &m_sharedString->string[offset]; + while ((ptr = std::strpbrk(ptr, oldCharacters)) != nullptr) { - if (!found) - { - std::ptrdiff_t offset = ptr - m_sharedString->string.get(); - - EnsureOwnership(); - - ptr = &m_sharedString->string[offset]; - found = true; - } - *ptr++ = replaceCharacter; ++count; } diff --git a/src/Nazara/Graphics/DepthRenderTechnique.cpp b/src/Nazara/Graphics/DepthRenderTechnique.cpp index 233eb9c72..8badefcc9 100644 --- a/src/Nazara/Graphics/DepthRenderTechnique.cpp +++ b/src/Nazara/Graphics/DepthRenderTechnique.cpp @@ -464,7 +464,6 @@ namespace Nz void DepthRenderTechnique::DrawOpaqueModels(const SceneData& sceneData, ForwardRenderQueue::Layer& layer) const { const Shader* lastShader = nullptr; - const ShaderUniforms* shaderUniforms = nullptr; for (auto& matIt : layer.opaqueModels) { @@ -488,7 +487,7 @@ namespace Nz if (shader != lastShader) { // Index of uniforms in the shader - shaderUniforms = GetShaderUniforms(shader); + GetShaderUniforms(shader); lastShader = shader; } diff --git a/tests/Engine/Core/String.cpp b/tests/Engine/Core/String.cpp index eac5eaef6..fe9c80c30 100644 --- a/tests/Engine/Core/String.cpp +++ b/tests/Engine/Core/String.cpp @@ -122,5 +122,25 @@ SCENARIO("String", "[CORE][STRING]") } } }*/ + + GIVEN("A string") + { + Nz::String replaceAny("abapeilomuky"); + Nz::String replaceAnyWithCase("abapEilOmuky"); + + WHEN("We replace any of vowels after character 3") + { + unsigned int nbrOfChanges = replaceAny.ReplaceAny("aeiouy", '$', 3); + unsigned int nbrOfChangesWithCase = replaceAnyWithCase.ReplaceAny("AEIOUY", '$', 3); + + THEN("These results are expected") + { + REQUIRE(replaceAny == "abap$$l$m$k$"); + REQUIRE(nbrOfChanges == 5); + REQUIRE(replaceAnyWithCase == "abap$il$muky"); + REQUIRE(nbrOfChangesWithCase == 2); + } + } + } }