From 1a801258020ba9e78002949a36130c7eb491455e Mon Sep 17 00:00:00 2001
From: Syping <schiedelrafael@keppe.org>
Date: Thu, 18 Jan 2024 19:53:47 +0100
Subject: [PATCH] CMakeLists and RagePhoto C file access improvements

- CMakeLists.txt: CMAKE_CXX_STANDARD -> RAGEPHOTO_CXX_STANDARD
- RagePhoto.c: Fix potential issues with file access
---
 CMakeLists.txt                | 12 ++++++++++--
 README.md                     |  2 +-
 cmake/cxxstd.cmake            | 21 ---------------------
 doc/build.doc                 |  2 +-
 src/core/RagePhoto.c          | 24 ++++++++++++------------
 src/core/RagePhotoConfig.h.in |  4 ++--
 6 files changed, 26 insertions(+), 39 deletions(-)
 delete mode 100644 cmake/cxxstd.cmake

diff --git a/CMakeLists.txt b/CMakeLists.txt
index f2dd767..d0a6abb 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1,6 +1,6 @@
 #[[**************************************************************************
 * libragephoto RAGE Photo Parser
-* Copyright (C) 2021-2023 Syping
+* Copyright (C) 2021-2024 Syping
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
@@ -21,7 +21,6 @@ project(ragephoto VERSION 0.6.0 LANGUAGES C CXX)
 include(GNUInstallDirs)
 
 # RagePhoto CMake includes
-include(cmake/cxxstd.cmake)
 include(cmake/unicode.cmake)
 
 # RagePhoto Top Level
@@ -88,6 +87,9 @@ else()
     endif()
 endif()
 
+# RagePhoto C++ Standard
+set(RAGEPHOTO_CXX_STANDARD "11" CACHE STRING "libragephoto C++ standard")
+
 # RagePhoto Win32 Shared Resources
 if (WIN32)
     string(TIMESTAMP ragephoto_BUILD_YEAR "%Y" UTC)
@@ -113,6 +115,10 @@ else()
         SOVERSION "${ragephoto_VERSION}"
     )
 endif()
+set_target_properties(ragephoto PROPERTIES
+    CXX_STANDARD ${RAGEPHOTO_CXX_STANDARD}
+    CXX_STANDARD_REQUIRED ON
+)
 target_compile_definitions(ragephoto PRIVATE
     LIBRAGEPHOTO_LIBRARY
     ${LIBRAGEPHOTO_DEFINES}
@@ -178,6 +184,8 @@ if (RAGEPHOTO_EXTRACT)
     add_executable(ragephoto-extract ${RAGEPHOTO_HEADERS} ${EXTRACT_SOURCES} ${EXTRACT_RESOURCES})
     set_target_properties(ragephoto-extract PROPERTIES
         INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}"
+        CXX_STANDARD ${RAGEPHOTO_CXX_STANDARD}
+        CXX_STANDARD_REQUIRED ON
     )
     if (MSVC AND MSVC_VERSION GREATER_EQUAL 1914)
         target_compile_options(ragephoto-extract PRIVATE $<$<COMPILE_LANGUAGE:CXX>:/Zc:__cplusplus>)
diff --git a/README.md b/README.md
index 7858009..4b9b3fe 100644
--- a/README.md
+++ b/README.md
@@ -16,7 +16,7 @@ sudo cmake --install libragephoto-build
 ```
 
 ##### Optional CMake flags
-`-DCMAKE_CXX_STANDARD=17`  
+`-DRAGEPHOTO_CXX_STANDARD=17`  
 `-DRAGEPHOTO_BENCHMARK=ON`  
 `-DRAGEPHOTO_C_API=OFF`  
 `-DRAGEPHOTO_C_LIBRARY=ON`  
diff --git a/cmake/cxxstd.cmake b/cmake/cxxstd.cmake
deleted file mode 100644
index 550b599..0000000
--- a/cmake/cxxstd.cmake
+++ /dev/null
@@ -1,21 +0,0 @@
-#[[**************************************************************************
-* libragephoto RAGE Photo Parser
-* Copyright (C) 2023 Syping
-*
-* Redistribution and use in source and binary forms, with or without modification,
-* are permitted provided that the following conditions are met:
-*
-* 1. Redistributions of source code must retain the above copyright notice,
-* this list of conditions and the following disclaimer.
-*
-* 2. Redistributions in binary form must reproduce the above copyright notice,
-* this list of conditions and the following disclaimer in the documentation
-* and/or other materials provided with the distribution.
-*
-* This software is provided as-is, no warranties are given to you, we are not
-* responsible for anything with use of the software, you are self responsible.
-****************************************************************************]]
-
-set(CMAKE_INCLUDE_CURRENT_DIR ON)
-set(CMAKE_CXX_STANDARD 11 CACHE STRING "libragephoto C++ standard")
-set(CMAKE_CXX_STANDARD_REQUIRED ON)
diff --git a/doc/build.doc b/doc/build.doc
index 0f68da9..0f7fab0 100644
--- a/doc/build.doc
+++ b/doc/build.doc
@@ -17,7 +17,7 @@ To customise your libragephoto build, the following <a href="#flags">Optional CM
 
 <h4 id="flags">Optional CMake flags</h4>
 \code{.sh}
--DCMAKE_CXX_STANDARD=17
+-DRAGEPHOTO_CXX_STANDARD=17
 -DRAGEPHOTO_BENCHMARK=ON
 -DRAGEPHOTO_C_API=OFF
 -DRAGEPHOTO_C_LIBRARY=ON
diff --git a/src/core/RagePhoto.c b/src/core/RagePhoto.c
index f740ea3..ff4326b 100644
--- a/src/core/RagePhoto.c
+++ b/src/core/RagePhoto.c
@@ -618,29 +618,29 @@ bool ragephoto_loadfile(ragephoto_t instance_t, const char *filename)
 #endif
     if (!file)
         return false;
-    const int64_t fseek_end = fseek(file, 0, SEEK_END);
-    if (fseek_end == -1) {
+    int fseek_ret = fseek(file, 0, SEEK_END);
+    if (!fseek_ret) {
         fclose(file);
         return false;
     }
-    const int64_t fileSize = ftell(file);
-    if (fileSize == -1) {
+    const long fileSize = ftell(file);
+    if (fileSize == -1L) {
         fclose(file);
         return false;
     }
-    const int64_t fseek_set = fseek(file, 0, SEEK_SET);
-    if (fseek_set == -1) {
+    fseek_ret = fseek(file, 0, SEEK_SET);
+    if (!fseek_ret) {
         fclose(file);
         return false;
     }
-    char *data = (char*)(malloc(fileSize));
+    char *data = (char*)malloc(fileSize);
     if (!data) {
         fclose(file);
         return false;
     }
-    const size_t fileRsize = fread(data, 1, fileSize, file);
+    const size_t readSize = fread(data, 1, fileSize, file);
     fclose(file);
-    if (fileSize != fileRsize) {
+    if (fileSize != readSize) {
         free(data);
         return false;
     }
@@ -986,7 +986,7 @@ bool ragephoto_savefilef(ragephoto_t instance_t, const char *filename, uint32_t
 {
     RagePhotoInstance *instance = (RagePhotoInstance*)instance_t;
     const size_t fileSize = ragephotodata_getsavesizef(instance->data, instance->parser, photoFormat);
-    char *data = (char*)(malloc(fileSize));
+    char *data = (char*)malloc(fileSize);
     if (!data)
         return false;
     if (!ragephotodata_savef(instance->data, instance->parser, data, photoFormat)) {
@@ -1003,10 +1003,10 @@ bool ragephoto_savefilef(ragephoto_t instance_t, const char *filename, uint32_t
         free(data);
         return false;
     }
-    const size_t fileWsize = fwrite(data, sizeof(char), fileSize, file);
+    const size_t writeSize = fwrite(data, sizeof(char), fileSize, file);
     fclose(file);
     free(data);
-    return (fileSize == fileWsize);
+    return (fileSize == writeSize);
 }
 
 bool ragephoto_savefile(ragephoto_t instance_t, const char *filename)
diff --git a/src/core/RagePhotoConfig.h.in b/src/core/RagePhotoConfig.h.in
index 03da1a2..ba83c38 100644
--- a/src/core/RagePhotoConfig.h.in
+++ b/src/core/RagePhotoConfig.h.in
@@ -1,6 +1,6 @@
 /*****************************************************************************
 * libragephoto RAGE Photo Parser
-* Copyright (C) 2023 Syping
+* Copyright (C) 2023-2024 Syping
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
@@ -22,7 +22,7 @@
 /* CMAKE CONFIG BEGIN */
 #define @LIBRAGEPHOTO_API@
 #define @LIBRAGEPHOTO_LIBTYPE@
-#define RAGEPHOTO_CXX_STD @CMAKE_CXX_STANDARD@
+#define RAGEPHOTO_CXX_STD @RAGEPHOTO_CXX_STANDARD@
 #define RAGEPHOTO_VERSION "@ragephoto_VERSION@"
 #define RAGEPHOTO_VERSION_MAJOR @ragephoto_VERSION_MAJOR@
 #define RAGEPHOTO_VERSION_MINOR @ragephoto_VERSION_MINOR@