Skip to content

Commit

Permalink
Prevent using 'unsafe' path functions from GDAL C++ code
Browse files Browse the repository at this point in the history
  • Loading branch information
rouault committed Jan 14, 2025
1 parent 887535a commit df669f9
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 13 deletions.
1 change: 1 addition & 0 deletions autotest/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ endif()
gdal_standard_includes(gdal_unit_test)
target_compile_options(gdal_unit_test PRIVATE ${GDAL_CXX_WARNING_FLAGS})
target_compile_definitions(gdal_unit_test PRIVATE -DGDAL_TEST_ROOT_DIR="${GDAL_ROOT_TEST_DIR}")
target_compile_definitions(gdal_unit_test PRIVATE ALLOW_DEPRECATED_CPL_PATH_FUNCTIONS)
target_include_directories(
gdal_unit_test PRIVATE $<TARGET_PROPERTY:appslib,SOURCE_DIR> $<TARGET_PROPERTY:gdal_vrt,SOURCE_DIR> $<TARGET_PROPERTY:ogrsf_generic,SOURCE_DIR>)
if (GDAL_USE_SQLITE3)
Expand Down
2 changes: 2 additions & 0 deletions port/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ target_sources(${GDAL_LIB_TARGET_NAME} PRIVATE $<TARGET_OBJECTS:cpl>)
target_compile_options(cpl PRIVATE ${GDAL_CXX_WARNING_FLAGS} ${WFLAG_OLD_STYLE_CAST} ${WFLAG_EFFCXX})
set_property(TARGET cpl PROPERTY POSITION_INDEPENDENT_CODE ${GDAL_OBJECT_LIBRARIES_POSITION_INDEPENDENT_CODE})

set_property(SOURCE cpl_path.cpp PROPERTY SKIP_UNITY_BUILD_INCLUSION ON)

# include path to generated cpl_config.h
target_include_directories(cpl PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR})

Expand Down
31 changes: 18 additions & 13 deletions port/cpl_conv.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,17 +151,18 @@ int CPL_DLL CPLGetExecPath(char *pszPathBuf, int nMaxLength);
/* -------------------------------------------------------------------- */
/* Filename handling functions. */
/* -------------------------------------------------------------------- */

#if defined(DOXYGEN_SKIP) || !defined(__cplusplus) || \
!defined(GDAL_COMPILATION) || \
(defined(__cplusplus) && defined(ALLOW_DEPRECATED_CPL_PATH_FUNCTIONS))
const char CPL_DLL *
CPLGetPath(const char *) CPL_WARN_UNUSED_RESULT CPL_RETURNS_NONNULL;
const char CPL_DLL *
CPLGetDirname(const char *) CPL_WARN_UNUSED_RESULT CPL_RETURNS_NONNULL;
const char CPL_DLL *
CPLGetFilename(const char *) CPL_WARN_UNUSED_RESULT CPL_RETURNS_NONNULL;
const char CPL_DLL *
CPLGetBasename(const char *) CPL_WARN_UNUSED_RESULT CPL_RETURNS_NONNULL;
const char CPL_DLL *
CPLGetExtension(const char *) CPL_WARN_UNUSED_RESULT CPL_RETURNS_NONNULL;
char CPL_DLL *CPLGetCurrentDir(void);
const char CPL_DLL *CPLFormFilename(
const char *pszPath, const char *pszBasename,
const char *pszExtension) CPL_WARN_UNUSED_RESULT CPL_RETURNS_NONNULL;
Expand All @@ -173,25 +174,29 @@ const char CPL_DLL *CPLResetExtension(const char *, const char *)
const char CPL_DLL *CPLProjectRelativeFilename(const char *pszProjectDir,
const char *pszSecondaryFilename)
CPL_WARN_UNUSED_RESULT CPL_RETURNS_NONNULL;
int CPL_DLL CPLIsFilenameRelative(const char *pszFilename);
const char CPL_DLL *CPLExtractRelativePath(const char *, const char *, int *)
CPL_WARN_UNUSED_RESULT CPL_RETURNS_NONNULL;
const char CPL_DLL *
CPLCleanTrailingSlash(const char *) CPL_WARN_UNUSED_RESULT CPL_RETURNS_NONNULL;

char CPL_DLL **
CPLCorrespondingPaths(const char *pszOldFilename, const char *pszNewFilename,
char **papszFileList) CPL_WARN_UNUSED_RESULT;
int CPL_DLL CPLCheckForFile(char *pszFilename, char **papszSiblingList);

const char CPL_DLL *CPLGenerateTempFilename(const char *pszStem)
CPL_WARN_UNUSED_RESULT CPL_RETURNS_NONNULL;
const char CPL_DLL *CPLExpandTilde(const char *pszFilename)
CPL_WARN_UNUSED_RESULT CPL_RETURNS_NONNULL;
const char CPL_DLL *CPLGetHomeDir(void) CPL_WARN_UNUSED_RESULT;
const char CPL_DLL *
CPLLaunderForFilename(const char *pszName,
const char *pszOutputPath) CPL_WARN_UNUSED_RESULT;
#endif

char CPL_DLL *CPLGetCurrentDir(void);
const char CPL_DLL *
CPLGetFilename(const char *) CPL_WARN_UNUSED_RESULT CPL_RETURNS_NONNULL;
int CPL_DLL CPLIsFilenameRelative(const char *pszFilename);
const char CPL_DLL *CPLExtractRelativePath(const char *, const char *, int *)
CPL_WARN_UNUSED_RESULT CPL_RETURNS_NONNULL;
char CPL_DLL **
CPLCorrespondingPaths(const char *pszOldFilename, const char *pszNewFilename,
char **papszFileList) CPL_WARN_UNUSED_RESULT;
int CPL_DLL CPLCheckForFile(char *pszFilename, char **papszSiblingList);

const char CPL_DLL *CPLGetHomeDir(void) CPL_WARN_UNUSED_RESULT;

#if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)

Expand Down
2 changes: 2 additions & 0 deletions port/cpl_path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
* SPDX-License-Identifier: MIT
****************************************************************************/

#define ALLOW_DEPRECATED_CPL_PATH_FUNCTIONS

#include "cpl_port.h"
#include "cpl_conv.h"

Expand Down

0 comments on commit df669f9

Please sign in to comment.