Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ Core » clamp() function crashes apps on Linux, gets warning from GCC and CLANG
clamp() function crashes apps on Linux, gets warning from GCC and CLANG [message #54576] Sat, 15 August 2020 23:10 Go to next message
Oblivion is currently offline  Oblivion
Messages: 1092
Registered: August 2007
Senior Contributor
Hi,

clamp function in the latest SVN build (rev. 14827) crashes apps.

GCC and CLANG complains about a reference to local variable addr.

Example:

#include <Core/Core.h>

using namespace Upp;

Size fn1()
{
	Size sz1(Random(100), Random(100));
	Size sz2(Random(1000), Random(1000));
	Size sz3(Random(1000), Random(1000));
	return clamp(sz1, sz2, sz3);
}

Size fn2()
{
	Size sz1(Random(100), Random(100));
	Size sz2(Random(1000), Random(1000));
	Size sz3(Random(1000), Random(1000));
	return min(max(sz1, sz2), sz3);
}

CONSOLE_APP_MAIN
{
	// both GCC and CLANG warns about reference to local-addr.

	Size sz1 = fn1(); // Crashes the app with invalid memory access.
	DUMP(sz1);
	Size sz2 = fn2(); // Does not crash.
	DUMP(sz2);
}


Best regards,
Oblivion


[Updated on: Sat, 15 August 2020 23:29]

Report message to a moderator

Re: clamp() function crashes apps on Linux, gets warning from GCC and CLANG [message #54577 is a reply to message #54576] Sun, 16 August 2020 12:27 Go to previous message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Oblivion wrote on Sat, 15 August 2020 23:10
Hi,

clamp function in the latest SVN build (rev. 14827) crashes apps.

GCC and CLANG complains about a reference to local variable addr.

Example:

#include <Core/Core.h>

using namespace Upp;

Size fn1()
{
	Size sz1(Random(100), Random(100));
	Size sz2(Random(1000), Random(1000));
	Size sz3(Random(1000), Random(1000));
	return clamp(sz1, sz2, sz3);
}

Size fn2()
{
	Size sz1(Random(100), Random(100));
	Size sz2(Random(1000), Random(1000));
	Size sz3(Random(1000), Random(1000));
	return min(max(sz1, sz2), sz3);
}

CONSOLE_APP_MAIN
{
	// both GCC and CLANG warns about reference to local-addr.

	Size sz1 = fn1(); // Crashes the app with invalid memory access.
	DUMP(sz1);
	Size sz2 = fn2(); // Does not crash.
	DUMP(sz2);
}


Best regards,
Oblivion


Interesting one. The real reason is that it goes to overloaded min/max.

For now fixed by changing singnature

template <class T>
constexpr T clamp(const T& x, const T& min_, const T& max_)


Hopefully this will not explode anywhere else...

Mirek
Previous Topic: Warning in Log.cpp
Next Topic: Vector initialization
Goto Forum:
  


Current Time: Thu Apr 18 21:08:25 CEST 2024

Total time taken to generate the page: 0.03447 seconds