Browse Source

Fix various code & correctness issues (#11815)

sfan5 2 years ago
parent
commit
ff934d538c

+ 1 - 1
CMakeLists.txt

@@ -26,7 +26,7 @@ set(DEVELOPMENT_BUILD TRUE)
 
 set(VERSION_STRING "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}")
 if(VERSION_EXTRA)
-	set(VERSION_STRING ${VERSION_STRING}-${VERSION_EXTRA})
+	set(VERSION_STRING "${VERSION_STRING}-${VERSION_EXTRA}")
 elseif(DEVELOPMENT_BUILD)
 	set(VERSION_STRING "${VERSION_STRING}-dev")
 endif()

+ 1 - 1
src/client/content_cao.cpp

@@ -850,7 +850,7 @@ void GenericCAO::addToScene(ITextureSource *tsrc, scene::ISceneManager *smgr)
 			logOnce(oss, warningstream);
 
 			video::ITexture *last = m_animated_meshnode->getMaterial(0).TextureLayer[0].Texture;
-			for (s32 i = 1; i < mat_count; i++) {
+			for (u32 i = 1; i < mat_count; i++) {
 				auto &layer = m_animated_meshnode->getMaterial(i).TextureLayer[0];
 				if (!layer.Texture)
 					layer.Texture = last;

+ 1 - 1
src/client/game.cpp

@@ -1383,7 +1383,7 @@ bool Game::createClient(const GameStartData &start_data)
 		str += L" [";
 		str += text;
 		str += L"]";
-		delete text;
+		delete[] text;
 	}
 	str += L" [";
 	str += driver->getName();

+ 1 - 2
src/gettext.h

@@ -48,8 +48,7 @@ void init_gettext(const char *path, const std::string &configured_language,
 
 extern wchar_t *utf8_to_wide_c(const char *str);
 
-// You must free the returned string!
-// The returned string is allocated using new
+// The returned string must be freed using delete[]
 inline const wchar_t *wgettext(const char *str)
 {
 	// We must check here that is not an empty string to avoid trying to translate it

+ 5 - 7
src/server.cpp

@@ -517,9 +517,7 @@ void Server::stop()
 
 	// Stop threads (set run=false first so both start stopping)
 	m_thread->stop();
-	//m_emergethread.setRun(false);
 	m_thread->wait();
-	//m_emergethread.stop();
 
 	infostream<<"Server: Threads stopped"<<std::endl;
 }
@@ -954,14 +952,14 @@ void Server::AsyncRunStep(bool initial_step)
 	}
 
 	/*
-		Trigger emergethread (it somehow gets to a non-triggered but
-		bysy state sometimes)
+		Trigger emerge thread
+		Doing this every 2s is left over from old code, unclear if this is still needed.
 	*/
 	{
 		float &counter = m_emergethread_trigger_timer;
-		counter += dtime;
-		if (counter >= 2.0) {
-			counter = 0.0;
+		counter -= dtime;
+		if (counter <= 0.0f) {
+			counter = 2.0f;
 
 			m_emerge->startThreads();
 		}

+ 1 - 1
src/settings.cpp

@@ -88,7 +88,7 @@ void SettingsHierarchy::onLayerCreated(int layer, Settings *obj)
 
 void SettingsHierarchy::onLayerRemoved(int layer)
 {
-	assert(layer >= 0 && layer < layers.size());
+	assert(layer >= 0 && layer < (int)layers.size());
 	layers[layer] = nullptr;
 	if (this == &g_hierarchy && layer == (int)SL_GLOBAL)
 		g_settings = nullptr;

+ 16 - 20
src/unittest/test_gettext.cpp

@@ -7,13 +7,12 @@ class TestGettext : public TestBase
 public:
 	TestGettext() {
 		TestManager::registerTestModule(this);
-  }
+	}
 
 	const char *getName() { return "TestGettext"; }
 
 	void runTests(IGameDef *gamedef);
 
-	void testSnfmtgettext();
 	void testFmtgettext();
 };
 
@@ -24,24 +23,21 @@ void TestGettext::runTests(IGameDef *gamedef)
 	TEST(testFmtgettext);
 }
 
+// Make sure updatepo.sh does not pick up the strings
+#define dummyname fmtgettext
+
 void TestGettext::testFmtgettext()
 {
-  std::string buf = fmtgettext("Viewing range changed to %d",  12);
-  UASSERTEQ(std::string, buf, "Viewing range changed to 12");
-  buf = fmtgettext(
-    "You are about to join this server with the name \"%s\" for the "
-    "first time.\n"
-    "If you proceed, a new account using your credentials will be "
-    "created on this server.\n"
-    "Please retype your password and click 'Register and Join' to "
-    "confirm account creation, or click 'Cancel' to abort."
-    ,  "A");
-  UASSERTEQ(std::string, buf,
-    "You are about to join this server with the name \"A\" for the "
-    "first time.\n"
-    "If you proceed, a new account using your credentials will be "
-    "created on this server.\n"
-    "Please retype your password and click 'Register and Join' to "
-    "confirm account creation, or click 'Cancel' to abort."
-  );
+	std::string buf = dummyname("sample text %d", 12);
+	UASSERTEQ(std::string, buf, "sample text 12");
+
+	std::string src, expect;
+	src    = "You are about to join this server with the name \"%s\".\n";
+	expect = "You are about to join this server with the name \"foo\".\n";
+	for (int i = 0; i < 20; i++) {
+		src.append("loooong text");
+		expect.append("loooong text");
+	}
+	buf = dummyname(src.c_str(), "foo");
+	UASSERTEQ(const std::string &, buf, expect);
 }

+ 4 - 4
src/unittest/test_utilities.cpp

@@ -392,9 +392,9 @@ void TestUtilities::testIsPowerOfTwo()
 	UASSERT(is_power_of_two(2) == true);
 	UASSERT(is_power_of_two(3) == false);
 	for (int exponent = 2; exponent <= 31; ++exponent) {
-		UASSERT(is_power_of_two((1 << exponent) - 1) == false);
-		UASSERT(is_power_of_two((1 << exponent)) == true);
-		UASSERT(is_power_of_two((1 << exponent) + 1) == false);
+		UASSERT(is_power_of_two((1U << exponent) - 1) == false);
+		UASSERT(is_power_of_two((1U << exponent)) == true);
+		UASSERT(is_power_of_two((1U << exponent) + 1) == false);
 	}
 	UASSERT(is_power_of_two(U32_MAX) == false);
 }
@@ -629,4 +629,4 @@ void TestUtilities::testBase64()
 	UASSERT(base64_is_valid("AAA=A") == false);
 	UASSERT(base64_is_valid("AAAA=A") == false);
 	UASSERT(base64_is_valid("AAAAA=A") == false);
-}
+}