Browse Source

Went through some Cppcheck warnings.

David Piuva 3 years ago
parent
commit
ea91d69baf

+ 7 - 7
Source/DFPSR/api/fileAPI.cpp

@@ -390,12 +390,12 @@ String file_getCurrentPath() {
 }
 
 String file_followSymbolicLink(const ReadableString &path, bool mustExist) {
-	String modifiedPath = file_optimizePath(path, LOCAL_PATH_SYNTAX);
-	Buffer buffer;
-	const NativeChar *nativePath = toNativeString(modifiedPath, buffer);
 	#ifdef USE_MICROSOFT_WINDOWS
 		// TODO: Is there anything that can be used as a symbolic link on Windows?
 	#else
+		String modifiedPath = file_optimizePath(path, LOCAL_PATH_SYNTAX);
+		Buffer buffer;
+		const NativeChar *nativePath = toNativeString(modifiedPath, buffer);
 		NativeChar resultBuffer[maxLength + 1] = {0};
 		if (readlink(nativePath, resultBuffer, maxLength) != -1) {
 			return fromNativeString(resultBuffer);
@@ -669,7 +669,7 @@ bool file_removeFile(const ReadableString& filename) {
 	bool result = false;
 	String optimizedPath = file_optimizePath(filename, LOCAL_PATH_SYNTAX);
 	Buffer buffer;
-	const NativeChar *nativePath = toNativeString(filename, buffer);
+	const NativeChar *nativePath = toNativeString(optimizedPath, buffer);
 	// Remove the empty folder.
 	#ifdef USE_MICROSOFT_WINDOWS
 		result = (DeleteFileW(nativePath) != 0);
@@ -735,8 +735,6 @@ DsrProcessStatus process_getStatus(const DsrProcess &process) {
 DsrProcess process_execute(const ReadableString& programPath, List<String> arguments) {
 	// Convert the program path into the native format.
 	String optimizedPath = file_optimizePath(programPath, LOCAL_PATH_SYNTAX);
-	Buffer pathBuffer;
-	const NativeChar *nativePath = toNativeString(optimizedPath, pathBuffer);
 	// Convert
 	#ifdef USE_MICROSOFT_WINDOWS
 		DsrChar separator = U' ';
@@ -744,7 +742,7 @@ DsrProcess process_execute(const ReadableString& programPath, List<String> argum
 		DsrChar separator = U'\0';
 	#endif
 	String flattenedArguments;
-	string_append(flattenedArguments, programPath);
+	string_append(flattenedArguments, optimizedPath);
 	string_appendChar(flattenedArguments, separator);
 	for (int64_t a = 0; a < arguments.length(); a++) {
 		string_append(flattenedArguments, arguments[a]);
@@ -764,6 +762,8 @@ DsrProcess process_execute(const ReadableString& programPath, List<String> argum
 			return DsrProcess(); // Failure
 		}
 	#else
+		Buffer pathBuffer;
+		const NativeChar *nativePath = toNativeString(optimizedPath, pathBuffer);
 		int64_t codePoints = buffer_getSize(argBuffer) / sizeof(NativeChar);
 		// Count arguments.
 		int argc = arguments.length() + 1;

+ 1 - 2
Source/DFPSR/api/imageAPI.cpp

@@ -128,8 +128,7 @@ bool dsr::image_save(const ImageRgbaU8 &image, const String& filename, bool must
 	ImageFileFormat extension = detectImageFileExtension(filename);
 	Buffer buffer;
 	if (extension == ImageFileFormat::Unknown) {
-		ReadableString extension = file_getExtension(filename);
-		if (mustWork) { throwError(U"The extension *.", extension, " in ", filename, " is not a supported image format.\n"); }
+		if (mustWork) { throwError(U"The extension *.", file_getExtension(filename), " in ", filename, " is not a supported image format.\n"); }
 		return false;
 	} else {
 		buffer = image_encode(image, extension, quality);

+ 1 - 2
Source/DFPSR/api/modelAPI.cpp

@@ -250,8 +250,7 @@ struct RendererImpl {
 	bool pointInsideOfEdge(const LVector2D &edgeA, const LVector2D &edgeB, const LVector2D &point) {
 		LVector2D edgeDirection = LVector2D(edgeB.y - edgeA.y, edgeA.x - edgeB.x);
 		LVector2D relativePosition = point - edgeA;
-		int64_t dotProduct = (edgeDirection.x * relativePosition.x) + (edgeDirection.y * relativePosition.y);
-		return dotProduct <= 0;
+		return (edgeDirection.x * relativePosition.x) + (edgeDirection.y * relativePosition.y) <= 0;
 	}
 	// Returns true iff the point is inside of the hull
 	// convexHullCorners from 0 to cornerCount-1 must be sorted clockwise and may not include any concave corners

+ 1 - 3
Source/DFPSR/api/stringAPI.cpp

@@ -313,8 +313,6 @@ String dsr::string_unmangleQuote(const ReadableString& mangledText) {
 				// Detect bad input
 				if (c == U'\"') { // Double quote
 					 throwError(U"Unmangled double quote sign detected in string_unmangleQuote!\n", mangledText, "\n");
-				} else if (c == U'\\') { // Back slash
-					 throwError(U"Unmangled back slash detected in string_unmangleQuote!\n", mangledText, "\n");
 				} else if (c == U'\a') { // Audible bell
 					 throwError(U"Unmangled audible bell detected in string_unmangleQuote!\n", mangledText, "\n");
 				} else if (c == U'\b') { // Backspace
@@ -794,7 +792,7 @@ static int64_t getNewBufferSize(int64_t minimumSize) {
 // Guarantees that the new buffer is not shared by other strings, so that it may be written to freely
 static void reallocateBuffer(String &target, int64_t newLength, bool preserve) {
 	// Holding oldData alive while copying to the new buffer
-	Buffer oldBuffer = target.buffer;
+	Buffer oldBuffer = target.buffer; // Kept for reference counting only, do not remove.
 	const char32_t* oldData = target.readSection;
 	target.buffer = buffer_create(getNewBufferSize(newLength * sizeof(DsrChar)));
 	target.readSection = target.writeSection = reinterpret_cast<char32_t*>(buffer_dangerous_getUnsafeData(target.buffer));

+ 13 - 6
Source/DFPSR/base/SafePointer.h

@@ -91,6 +91,13 @@ public:
 		#endif
 		return this->data;
 	}
+	// Get unsafe pointer without bound checks for implementing your own safety
+	inline T* getUnchecked() {
+		return this->data;
+	}
+	inline const T* getUnchecked() const {
+		return this->data;
+	}
 	// Returns the pointer in modulo byteAlignment
 	// Returns 0 if the pointer is aligned with byteAlignment
 	inline int getAlignmentOffset(int byteAlignment) const {
@@ -219,22 +226,22 @@ template <typename T, typename S>
 inline void safeMemoryCopy(SafePointer<T> target, const SafePointer<S>& source, int byteSize) {
 	#ifdef SAFE_POINTER_CHECKS
 		// Both target and source must be in valid memory
-		target.assertInside("memoryCopy (target)", target.getUnsafe(), (size_t)byteSize);
-		source.assertInside("memoryCopy (source)", source.getUnsafe(), (size_t)byteSize);
+		target.assertInside("memoryCopy (target)", target.getUnchecked(), (size_t)byteSize);
+		source.assertInside("memoryCopy (source)", source.getUnchecked(), (size_t)byteSize);
 		// memcpy doesn't allow pointer aliasing
 		// TODO: Make a general assertion with the same style as out of bound exceptions
-		assert(((const uint8_t*)target.getUnsafe()) + byteSize <= (uint8_t*)source.getUnsafe() || ((const uint8_t*)source.getUnsafe()) + byteSize <= (uint8_t*)target.getUnsafe());
+		assert(((const uint8_t*)target.getUnchecked()) + byteSize <= (uint8_t*)source.getUnchecked() || ((const uint8_t*)source.getUnchecked()) + byteSize <= (uint8_t*)target.getUnchecked());
 	#endif
-	std::memcpy(target.getUnsafe(), source.getUnsafe(), (size_t)byteSize);
+	std::memcpy(target.getUnchecked(), source.getUnchecked(), (size_t)byteSize);
 }
 
 template <typename T>
 inline void safeMemorySet(SafePointer<T>& target, uint8_t value, int byteSize) {
 	#ifdef SAFE_POINTER_CHECKS
 		// Target must be in valid memory
-		target.assertInside("memoryCopy (target)", target.getUnsafe(), byteSize);
+		target.assertInside("memoryCopy (target)", target.getUnchecked(), byteSize);
 	#endif
-	std::memset((char*)(target.getUnsafe()), value, (size_t)byteSize);
+	std::memset((char*)(target.getUnchecked()), value, (size_t)byteSize);
 }
 
 }

+ 0 - 4
Source/DFPSR/gui/components/ListBox.cpp

@@ -301,10 +301,6 @@ int64_t ListBox::getVisibleScrollRange() {
 	return (this->location.height() - textBorderTop * 2) / verticalStep;
 }
 
-IRect ListBox::getScrollBarLocation_includingButtons() {
-	return IRect(this->location.width() - scrollWidth, 0, scrollWidth, this->location.height());
-}
-
 IRect ListBox::getScrollBarLocation_excludingButtons() {
 	return IRect(this->location.width() - scrollWidth, scrollEndHeight, scrollWidth, this->location.height() - (scrollEndHeight * 2));
 }

+ 2 - 1
Source/DFPSR/image/ImageRgbaU8.cpp

@@ -30,7 +30,7 @@ using namespace dsr;
 
 IMAGE_DEFINITION(ImageRgbaU8Impl, 4, Color4xU8, uint8_t);
 
-ImageRgbaU8Impl::ImageRgbaU8Impl(int32_t newWidth, int32_t newHeight, int32_t newStride, Buffer buffer, intptr_t startOffset, PackOrder packOrder) :
+ImageRgbaU8Impl::ImageRgbaU8Impl(int32_t newWidth, int32_t newHeight, int32_t newStride, Buffer buffer, intptr_t startOffset, const PackOrder &packOrder) :
   ImageImpl(newWidth, newHeight, newStride, sizeof(Color4xU8), buffer, startOffset), packOrder(packOrder) {
 	assert(buffer_getSize(buffer) - startOffset >= imageInternal::getUsedBytes(this));
 	this->initializeRgbaImage();
@@ -231,6 +231,7 @@ void ImageRgbaU8Impl::generatePyramid() {
 		}
 		// Fill unused mip levels with duplicates of the last mip level
 		for (int32_t m = mipmaps; m < MIP_BIN_COUNT; m++) {
+			// m - 1 is never negative, because mipmaps is clamped to at least 1 and nobody would choose zero for MIP_BIN_COUNT.
 			this->texture.mips[m] = this->texture.mips[m - 1];
 		}
 	}

+ 1 - 1
Source/DFPSR/image/ImageRgbaU8.h

@@ -64,7 +64,7 @@ public:
 	// Macro defined functions
 	IMAGE_DECLARATION(ImageRgbaU8Impl, 4, Color4xU8, uint8_t);
 	// Constructors
-	ImageRgbaU8Impl(int32_t newWidth, int32_t newHeight, int32_t newStride, Buffer buffer, intptr_t startOffset, PackOrder packOrder);
+	ImageRgbaU8Impl(int32_t newWidth, int32_t newHeight, int32_t newStride, Buffer buffer, intptr_t startOffset, const PackOrder &packOrder);
 	ImageRgbaU8Impl(int32_t newWidth, int32_t newHeight, int32_t alignment = 16);
 	// Native canvas constructor
 	ImageRgbaU8Impl(int32_t newWidth, int32_t newHeight, PackOrderIndex packOrderIndex);

+ 36 - 49
Source/DFPSR/image/draw.cpp

@@ -670,14 +670,13 @@ void dsr::imageImpl_drawHigher(ImageU16Impl& targetHeight, const ImageU16Impl& s
 	if (ImageIntersection::canCreate(targetHeight, sourceHeight, left, top)) {
 		ImageIntersection intersectionH = ImageIntersection::create(targetHeight, sourceHeight, left, top);
 		ITERATE_PIXELS(intersectionH.subTarget, intersectionH.subSource,
-			int32_t sourceHeight = *((const uint16_t*)sourcePixel);
-			if (sourceHeight > 0) {
-				sourceHeight += sourceHeightOffset;
-				int32_t targetHeight = *((uint16_t*)targetPixel);
-				if (sourceHeight < 0) { sourceHeight = 0; }
-				if (sourceHeight > 65535) { sourceHeight = 65535; }
-				if (sourceHeight > 0 && sourceHeight > targetHeight) {
-					*((uint16_t*)targetPixel) = sourceHeight;
+			int32_t newHeight = *((const uint16_t*)sourcePixel);
+			if (newHeight > 0) {
+				newHeight += sourceHeightOffset;
+				if (newHeight < 0) { newHeight = 0; }
+				if (newHeight > 65535) { newHeight = 65535; }
+				if (newHeight > 0 && newHeight > *((uint16_t*)targetPixel)) {
+					*((uint16_t*)targetPixel) = newHeight;
 				}
 			}
 		);
@@ -691,14 +690,13 @@ void dsr::imageImpl_drawHigher(ImageU16Impl& targetHeight, const ImageU16Impl& s
 		ImageIntersection intersectionH = ImageIntersection::create(targetHeight, sourceHeight, left, top);
 		ImageIntersection intersectionA = ImageIntersection::create(targetA, sourceA, left, top);
 		ITERATE_PIXELS_2(intersectionH.subTarget, intersectionH.subSource, intersectionA.subTarget, intersectionA.subSource,
-			int32_t sourceHeight = *((const uint16_t*)sourcePixel1);
-			if (sourceHeight > 0) {
-				sourceHeight += sourceHeightOffset;
-				int32_t targetHeight = *((uint16_t*)targetPixel1);
-				if (sourceHeight < 0) { sourceHeight = 0; }
-				if (sourceHeight > 65535) { sourceHeight = 65535; }
-				if (sourceHeight > targetHeight) {
-					*((uint16_t*)targetPixel1) = sourceHeight;
+			int32_t newHeight = *((const uint16_t*)sourcePixel1);
+			if (newHeight > 0) {
+				newHeight += sourceHeightOffset;
+				if (newHeight < 0) { newHeight = 0; }
+				if (newHeight > 65535) { newHeight = 65535; }
+				if (newHeight > *((uint16_t*)targetPixel1)) {
+					*((uint16_t*)targetPixel1) = newHeight;
 					targetPixel2[targetA.packOrder.redIndex]   = sourcePixel2[sourceA.packOrder.redIndex];
 					targetPixel2[targetA.packOrder.greenIndex] = sourcePixel2[sourceA.packOrder.greenIndex];
 					targetPixel2[targetA.packOrder.blueIndex]  = sourcePixel2[sourceA.packOrder.blueIndex];
@@ -719,14 +717,13 @@ void dsr::imageImpl_drawHigher(ImageU16Impl& targetHeight, const ImageU16Impl& s
 		ImageIntersection intersectionA = ImageIntersection::create(targetA, sourceA, left, top);
 		ImageIntersection intersectionB = ImageIntersection::create(targetB, sourceB, left, top);
 		ITERATE_PIXELS_3(intersectionH.subTarget, intersectionH.subSource, intersectionA.subTarget, intersectionA.subSource, intersectionB.subTarget, intersectionB.subSource,
-			int32_t sourceHeight = *((const uint16_t*)sourcePixel1);
-			if (sourceHeight > 0) {
-				sourceHeight += sourceHeightOffset;
-				int32_t targetHeight = *((uint16_t*)targetPixel1);
-				if (sourceHeight < 0) { sourceHeight = 0; }
-				if (sourceHeight > 65535) { sourceHeight = 65535; }
-				if (sourceHeight > targetHeight) {
-					*((uint16_t*)targetPixel1) = sourceHeight;
+			int32_t newHeight = *((const uint16_t*)sourcePixel1);
+			if (newHeight > 0) {
+				newHeight += sourceHeightOffset;
+				if (newHeight < 0) { newHeight = 0; }
+				if (newHeight > 65535) { newHeight = 65535; }
+				if (newHeight > *((uint16_t*)targetPixel1)) {
+					*((uint16_t*)targetPixel1) = newHeight;
 					targetPixel2[targetA.packOrder.redIndex]   = sourcePixel2[sourceA.packOrder.redIndex];
 					targetPixel2[targetA.packOrder.greenIndex] = sourcePixel2[sourceA.packOrder.greenIndex];
 					targetPixel2[targetA.packOrder.blueIndex]  = sourcePixel2[sourceA.packOrder.blueIndex];
@@ -745,12 +742,11 @@ void dsr::imageImpl_drawHigher(ImageF32Impl& targetHeight, const ImageF32Impl& s
 	if (ImageIntersection::canCreate(targetHeight, sourceHeight, left, top)) {
 		ImageIntersection intersectionH = ImageIntersection::create(targetHeight, sourceHeight, left, top);
 		ITERATE_PIXELS(intersectionH.subTarget, intersectionH.subSource,
-			float sourceHeight = *((const float*)sourcePixel);
-			if (sourceHeight > -std::numeric_limits<float>::infinity()) {
-				sourceHeight += sourceHeightOffset;
-				float targetHeight = *((float*)targetPixel);
-				if (sourceHeight > targetHeight) {
-					*((float*)targetPixel) = sourceHeight;
+			float newHeight = *((const float*)sourcePixel);
+			if (newHeight > -std::numeric_limits<float>::infinity()) {
+				newHeight += sourceHeightOffset;
+				if (newHeight > *((float*)targetPixel)) {
+					*((float*)targetPixel) = newHeight;
 				}
 			}
 		);
@@ -764,12 +760,11 @@ void dsr::imageImpl_drawHigher(ImageF32Impl& targetHeight, const ImageF32Impl& s
 		ImageIntersection intersectionH = ImageIntersection::create(targetHeight, sourceHeight, left, top);
 		ImageIntersection intersectionA = ImageIntersection::create(targetA, sourceA, left, top);
 		ITERATE_PIXELS_2(intersectionH.subTarget, intersectionH.subSource, intersectionA.subTarget, intersectionA.subSource,
-			float sourceHeight = *((const float*)sourcePixel1);
-			if (sourceHeight > -std::numeric_limits<float>::infinity()) {
-				sourceHeight += sourceHeightOffset;
-				float targetHeight = *((float*)targetPixel1);
-				if (sourceHeight > targetHeight) {
-					*((float*)targetPixel1) = sourceHeight;
+			float newHeight = *((const float*)sourcePixel1);
+			if (newHeight > -std::numeric_limits<float>::infinity()) {
+				newHeight += sourceHeightOffset;
+				if (newHeight > *((float*)targetPixel1)) {
+					*((float*)targetPixel1) = newHeight;
 					targetPixel2[targetA.packOrder.redIndex]   = sourcePixel2[sourceA.packOrder.redIndex];
 					targetPixel2[targetA.packOrder.greenIndex] = sourcePixel2[sourceA.packOrder.greenIndex];
 					targetPixel2[targetA.packOrder.blueIndex]  = sourcePixel2[sourceA.packOrder.blueIndex];
@@ -790,12 +785,11 @@ void dsr::imageImpl_drawHigher(ImageF32Impl& targetHeight, const ImageF32Impl& s
 		ImageIntersection intersectionA = ImageIntersection::create(targetA, sourceA, left, top);
 		ImageIntersection intersectionB = ImageIntersection::create(targetB, sourceB, left, top);
 		ITERATE_PIXELS_3(intersectionH.subTarget, intersectionH.subSource, intersectionA.subTarget, intersectionA.subSource, intersectionB.subTarget, intersectionB.subSource,
-			float sourceHeight = *((const float*)sourcePixel1);
-			if (sourceHeight > -std::numeric_limits<float>::infinity()) {
-				sourceHeight += sourceHeightOffset;
-				float targetHeight = *((float*)targetPixel1);
-				if (sourceHeight > targetHeight) {
-					*((float*)targetPixel1) = sourceHeight;
+			float newHeight = *((const float*)sourcePixel1);
+			if (newHeight > -std::numeric_limits<float>::infinity()) {
+				newHeight += sourceHeightOffset;
+				if (newHeight > *((float*)targetPixel1)) {
+					*((float*)targetPixel1) = newHeight;
 					targetPixel2[targetA.packOrder.redIndex]   = sourcePixel2[sourceA.packOrder.redIndex];
 					targetPixel2[targetA.packOrder.greenIndex] = sourcePixel2[sourceA.packOrder.greenIndex];
 					targetPixel2[targetA.packOrder.blueIndex]  = sourcePixel2[sourceA.packOrder.blueIndex];
@@ -810,13 +804,6 @@ void dsr::imageImpl_drawHigher(ImageF32Impl& targetHeight, const ImageF32Impl& s
 	}
 }
 
-/*
-void imageImpl_drawHigher(ImageF32Impl& targetHeight, const ImageF32Impl& sourceHeight, int32_t left = 0, int32_t top = 0, float sourceHeightOffset = 0);
-void imageImpl_drawHigher(ImageF32Impl& targetHeight, const ImageF32Impl& sourceHeight, ImageRgbaU8Impl& targetA, const ImageRgbaU8Impl& sourceA,
-  int32_t left = 0, int32_t top = 0, float sourceHeightOffset = 0);
-void imageImpl_drawHigher(ImageF32Impl& targetHeight, const ImageF32Impl& sourceHeight, ImageRgbaU8Impl& targetA, const ImageRgbaU8Impl& sourceA,
-  ImageRgbaU8Impl& targetB, const ImageRgbaU8Impl& sourceB, int32_t left = 0, int32_t top = 0, float sourceHeightOffset = 0);
-*/
 
 // -------------------------------- Resize --------------------------------
 

+ 2 - 2
Source/DFPSR/render/model/Model.h

@@ -42,7 +42,7 @@ struct VertexData {
 	FVector4D texCoord; // Two 2D coordinates or one 3D coordinate
 	FVector4D color; // RGBA
 	VertexData() : texCoord(FVector4D(0.0f, 0.0f, 0.0f, 0.0f)), color(FVector4D(1.0f, 1.0f, 1.0f, 1.0f)) {}
-	VertexData(FVector4D texCoord, FVector4D color) : texCoord(texCoord), color(color) {}
+	VertexData(const FVector4D &texCoord, const FVector4D &color) : texCoord(texCoord), color(color) {}
 };
 
 // Only used when constructing new polygons
@@ -50,7 +50,7 @@ struct Vertex {
 	int32_t pointIndex = -1; // Empty
 	VertexData data;
 	Vertex() {}
-	Vertex(int32_t pointIndex, VertexData data) : pointIndex(pointIndex), data(data) {}
+	Vertex(int32_t pointIndex, const VertexData &data) : pointIndex(pointIndex), data(data) {}
 };
 
 struct Polygon {

+ 1 - 1
Source/DFPSR/render/renderCore.h

@@ -47,7 +47,7 @@ struct TriangleDrawData {
 	TriangleInput triangleInput;
 	// Function pointer to the method that will process the command
 	DRAW_CALLBACK_TYPE processTriangle;
-	TriangleDrawData(ImageRgbaU8Impl *targetImage, ImageF32Impl *depthBuffer, bool perspective, Filter filter, TriangleInput triangleInput, DRAW_CALLBACK_TYPE processTriangle)
+	TriangleDrawData(ImageRgbaU8Impl *targetImage, ImageF32Impl *depthBuffer, bool perspective, Filter filter, const TriangleInput &triangleInput, DRAW_CALLBACK_TYPE processTriangle)
 	: targetImage(targetImage), depthBuffer(depthBuffer), perspective(perspective), filter(filter), triangleInput(triangleInput), processTriangle(processTriangle) {}
 };