[LTP] [PATCH 2/7] lib: safe_macros: Add SAFE_MREMAP

Andrea Cervesato andrea.cervesato@suse.com
Wed Jul 9 15:16:02 CEST 2025


Hi!

On 7/8/25 10:11 PM, Ricardo B. Marlière via ltp wrote:
> From: Ricardo B. Marlière <rbm@suse.com>
>
> Signed-off-by: Ricardo B. Marlière <rbm@suse.com>
> ---
>   include/safe_macros_fn.h  |  2 ++
>   include/tst_safe_macros.h |  4 ++++
>   lib/safe_macros.c         | 16 ++++++++++++++++
>   3 files changed, 22 insertions(+)
>
> diff --git a/include/safe_macros_fn.h b/include/safe_macros_fn.h
> index d256091b76ad10b06b29e3fd5fad8853faa14c08..4892706fa311ae23131cd1ba3d36e8eb3257d9c4 100644
> --- a/include/safe_macros_fn.h
> +++ b/include/safe_macros_fn.h
> @@ -69,6 +69,8 @@ int safe_mkdir(const char *file, const int lineno,
>   int safe_rmdir(const char *file, const int lineno,
>                  void (*cleanup_fn)(void), const char *pathname);
>   
> +void* safe_mremap(const char *file, const int lineno, void (*cleanup_fn)(void),
> +		void *old_address, size_t old_size, size_t new_size, int flags);
>   
>   int safe_munmap(const char *file, const int lineno,
>                   void (*cleanup_fn)(void), void *addr, size_t length);
> diff --git a/include/tst_safe_macros.h b/include/tst_safe_macros.h
> index 19504beb57ad379c835a13ed5d35fe06e42a6ed6..9ca70319f086dea437467c775dd9cb48b956583e 100644
> --- a/include/tst_safe_macros.h
> +++ b/include/tst_safe_macros.h
> @@ -93,6 +93,10 @@ void *safe_realloc(const char *file, const int lineno, void *ptr, size_t size);
>   #define SAFE_RMDIR(pathname) \
>   	safe_rmdir(__FILE__, __LINE__, NULL, (pathname))
>   
> +#define SAFE_MREMAP(old_address, old_size, new_size, flags)              \
> +	safe_mremap(__FILE__, __LINE__, NULL, (old_address), (old_size), \
> +		    (new_size), (flags))
> +
>   #define SAFE_MUNMAP(addr, length) \
>   	safe_munmap(__FILE__, __LINE__, NULL, (addr), (length))
>   
> diff --git a/lib/safe_macros.c b/lib/safe_macros.c
> index 57bc0bc749712f1b890594123b6b9f3cabce821a..4bb4c700f1d25e9520e3d5a2f9969bfecafd25cc 100644
> --- a/lib/safe_macros.c
> +++ b/lib/safe_macros.c
> @@ -215,6 +215,22 @@ int safe_rmdir(const char *file, const int lineno, void (*cleanup_fn) (void),
>   	return (rval);
>   }
>   
> +void *safe_mremap(const char *file, const int lineno, void (*cleanup_fn)(void),
> +		void *old_address, size_t old_size, size_t new_size, int flags)
> +{
> +	void *rval;
> +
> +	rval = mremap(old_address, old_size, new_size, flags);
> +
> +	if (rval == MAP_FAILED) {
> +		tst_brkm_(file, lineno, TBROK | TERRNO, cleanup_fn,
> +			  "mremap(%p,%zu,%zu,%d) failed", old_address, old_size,
> +			  new_size, flags);
> +	}
Here we can handle also unexpected return values. Please check safe_munmap()
> +
> +	return rval;
> +}
> +
>   int safe_munmap(const char *file, const int lineno, void (*cleanup_fn) (void),
>                   void *addr, size_t length)
>   {
>
- Andrea


More information about the ltp mailing list