Skip to content

Commit

Permalink
Move overflow macros from VFDs to H5FDpkg.h (#5084)
Browse files Browse the repository at this point in the history
These are identical in all VFDs except the core VFD, so they've
been moved to H5FDpkg.h and renamed:

    * MAXADDR           -->     H5FD_MAXADDR
    * ADDR_OVERFLOW     -->     H5FD_ADDR_OVERFLOW
    * SIZE_OVERFLOW     -->     H5FD_SIZE_OVERFLOW
    * REGION_OVERFLOW   -->     H5FD_REGION_OVERFLOW
  • Loading branch information
derobins authored Nov 8, 2024
1 parent 0d716c0 commit faec68f
Show file tree
Hide file tree
Showing 13 changed files with 88 additions and 233 deletions.
35 changes: 18 additions & 17 deletions src/H5FDcore.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,21 +103,22 @@ typedef struct H5FD_core_fapl_t {
/* These macros check for overflow of various quantities. These macros
* assume that file_offset_t is signed and haddr_t and size_t are unsigned.
*
* ADDR_OVERFLOW: Checks whether a file address of type `haddr_t'
* is too large to be represented by the second argument
* of the file seek function.
* CORE_ADDR_OVERFLOW: Checks whether a file address of type `haddr_t'
* is too large to be represented by the second argument
* of the file seek function.
*
* SIZE_OVERFLOW: Checks whether a buffer size of type `hsize_t' is too
* large to be represented by the `size_t' type.
* CORE_SIZE_OVERFLOW: Checks whether a buffer size of type `hsize_t' is too
* large to be represented by the `size_t' type.
*
* REGION_OVERFLOW: Checks whether an address and size pair describe data
* which can be addressed entirely in memory.
* CORE_REGION_OVERFLOW: Checks whether an address and size pair describe data
* which can be addressed entirely in memory.
*/
#define MAXADDR ((haddr_t)((~(size_t)0) - 1))
#define ADDR_OVERFLOW(A) (HADDR_UNDEF == (A) || (A) > (haddr_t)MAXADDR)
#define SIZE_OVERFLOW(Z) ((Z) > (hsize_t)MAXADDR)
#define REGION_OVERFLOW(A, Z) \
(ADDR_OVERFLOW(A) || SIZE_OVERFLOW(Z) || HADDR_UNDEF == (A) + (Z) || (size_t)((A) + (Z)) < (size_t)(A))
#define CORE_MAXADDR ((haddr_t)((~(size_t)0) - 1))
#define CORE_ADDR_OVERFLOW(A) (HADDR_UNDEF == (A) || (A) > (haddr_t)CORE_MAXADDR)
#define CORE_SIZE_OVERFLOW(Z) ((Z) > (hsize_t)CORE_MAXADDR)
#define CORE_REGION_OVERFLOW(A, Z) \
(CORE_ADDR_OVERFLOW(A) || CORE_SIZE_OVERFLOW(Z) || HADDR_UNDEF == (A) + (Z) || \
(size_t)((A) + (Z)) < (size_t)(A))

/* Prototypes */
static herr_t H5FD__core_add_dirty_region(H5FD_core_t *file, haddr_t start, haddr_t end);
Expand Down Expand Up @@ -147,7 +148,7 @@ static const H5FD_class_t H5FD_core_g = {
H5FD_CLASS_VERSION, /* struct version */
H5FD_CORE_VALUE, /* value */
"core", /* name */
MAXADDR, /* maxaddr */
CORE_MAXADDR, /* maxaddr */
H5F_CLOSE_WEAK, /* fc_degree */
NULL, /* terminate */
NULL, /* sb_size */
Expand Down Expand Up @@ -707,7 +708,7 @@ H5FD__core_open(const char *name, unsigned flags, hid_t fapl_id, haddr_t maxaddr
HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, NULL, "invalid file name");
if (0 == maxaddr || HADDR_UNDEF == maxaddr)
HGOTO_ERROR(H5E_ARGS, H5E_BADRANGE, NULL, "bogus maxaddr");
if (ADDR_OVERFLOW(maxaddr))
if (CORE_ADDR_OVERFLOW(maxaddr))
HGOTO_ERROR(H5E_ARGS, H5E_OVERFLOW, NULL, "maxaddr overflow");
assert(H5P_DEFAULT != fapl_id);
if (NULL == (plist = (H5P_genplist_t *)H5I_object(fapl_id)))
Expand Down Expand Up @@ -1155,7 +1156,7 @@ H5FD__core_set_eoa(H5FD_t *_file, H5FD_mem_t H5_ATTR_UNUSED type, haddr_t addr)

FUNC_ENTER_PACKAGE

if (ADDR_OVERFLOW(addr))
if (CORE_ADDR_OVERFLOW(addr))
HGOTO_ERROR(H5E_ARGS, H5E_OVERFLOW, FAIL, "address overflow");

file->eoa = addr;
Expand Down Expand Up @@ -1271,7 +1272,7 @@ H5FD__core_read(H5FD_t *_file, H5FD_mem_t H5_ATTR_UNUSED type, hid_t H5_ATTR_UNU
/* Check for overflow conditions */
if (HADDR_UNDEF == addr)
HGOTO_ERROR(H5E_IO, H5E_OVERFLOW, FAIL, "file address overflowed");
if (REGION_OVERFLOW(addr, size))
if (CORE_REGION_OVERFLOW(addr, size))
HGOTO_ERROR(H5E_IO, H5E_OVERFLOW, FAIL, "file address overflowed");

/* Read the part which is before the EOF marker */
Expand Down Expand Up @@ -1325,7 +1326,7 @@ H5FD__core_write(H5FD_t *_file, H5FD_mem_t H5_ATTR_UNUSED type, hid_t H5_ATTR_UN
assert(buf);

/* Check for overflow conditions */
if (REGION_OVERFLOW(addr, size))
if (CORE_REGION_OVERFLOW(addr, size))
HGOTO_ERROR(H5E_IO, H5E_OVERFLOW, FAIL, "file address overflowed");

/*
Expand Down
29 changes: 4 additions & 25 deletions src/H5FDdirect.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,27 +90,6 @@ typedef struct H5FD_direct_t {

} H5FD_direct_t;

/*
* These macros check for overflow of various quantities. These macros
* assume that HDoff_t is signed and haddr_t and size_t are unsigned.
*
* ADDR_OVERFLOW: Checks whether a file address of type `haddr_t'
* is too large to be represented by the second argument
* of the file seek function.
*
* SIZE_OVERFLOW: Checks whether a buffer size of type `hsize_t' is too
* large to be represented by the `size_t' type.
*
* REGION_OVERFLOW: Checks whether an address and size pair describe data
* which can be addressed entirely by the second
* argument of the file seek function.
*/
#define MAXADDR (((haddr_t)1 << (8 * sizeof(HDoff_t) - 1)) - 1)
#define ADDR_OVERFLOW(A) (HADDR_UNDEF == (A) || ((A) & ~(haddr_t)MAXADDR))
#define SIZE_OVERFLOW(Z) ((Z) & ~(hsize_t)MAXADDR)
#define REGION_OVERFLOW(A, Z) \
(ADDR_OVERFLOW(A) || SIZE_OVERFLOW(Z) || HADDR_UNDEF == (A) + (Z) || (HDoff_t)((A) + (Z)) < (HDoff_t)(A))

/* Prototypes */
static herr_t H5FD__direct_populate_config(size_t boundary, size_t block_size, size_t cbuf_size,
H5FD_direct_fapl_t *fa_out);
Expand All @@ -137,7 +116,7 @@ static const H5FD_class_t H5FD_direct_g = {
H5FD_CLASS_VERSION, /* struct version */
H5FD_DIRECT_VALUE, /* value */
"direct", /* name */
MAXADDR, /* maxaddr */
H5FD_MAXADDR, /* maxaddr */
H5F_CLOSE_WEAK, /* fc_degree */
NULL, /* terminate */
NULL, /* sb_size */
Expand Down Expand Up @@ -436,7 +415,7 @@ H5FD__direct_open(const char *name, unsigned flags, hid_t fapl_id, haddr_t maxad
HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, NULL, "invalid file name");
if (0 == maxaddr || HADDR_UNDEF == maxaddr)
HGOTO_ERROR(H5E_ARGS, H5E_BADRANGE, NULL, "bogus maxaddr");
if (ADDR_OVERFLOW(maxaddr))
if (H5FD_ADDR_OVERFLOW(maxaddr))
HGOTO_ERROR(H5E_ARGS, H5E_OVERFLOW, NULL, "bogus maxaddr");

/* Build the open flags */
Expand Down Expand Up @@ -818,7 +797,7 @@ H5FD__direct_read(H5FD_t *_file, H5FD_mem_t H5_ATTR_UNUSED type, hid_t H5_ATTR_U
/* Check for overflow conditions */
if (HADDR_UNDEF == addr)
HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "addr undefined");
if (REGION_OVERFLOW(addr, size))
if (H5FD_REGION_OVERFLOW(addr, size))
HGOTO_ERROR(H5E_ARGS, H5E_OVERFLOW, FAIL, "addr overflow");

/* If the system doesn't require data to be aligned, read the data in
Expand Down Expand Up @@ -998,7 +977,7 @@ H5FD__direct_write(H5FD_t *_file, H5FD_mem_t H5_ATTR_UNUSED type, hid_t H5_ATTR_
/* Check for overflow conditions */
if (HADDR_UNDEF == addr)
HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "addr undefined");
if (REGION_OVERFLOW(addr, size))
if (H5FD_REGION_OVERFLOW(addr, size))
HGOTO_ERROR(H5E_ARGS, H5E_OVERFLOW, FAIL, "addr overflow");

/* If the system doesn't require data to be aligned, read the data in
Expand Down
17 changes: 2 additions & 15 deletions src/H5FDhdfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,19 +230,6 @@ typedef struct H5FD_hdfs_t {
#endif
} H5FD_hdfs_t;

/*
* These macros check for overflow of various quantities. These macros
* assume that HDoff_t is signed and haddr_t and size_t are unsigned.
*
* ADDR_OVERFLOW: Checks whether a file address of type `haddr_t'
* is too large to be represented by the second argument
* of the file seek function.
* Only included if HDFS code should compile.
*
*/
#define MAXADDR (((haddr_t)1 << (8 * sizeof(HDoff_t) - 1)) - 1)
#define ADDR_OVERFLOW(A) (HADDR_UNDEF == (A) || ((A) & ~(haddr_t)MAXADDR))

/* Prototypes */
static void *H5FD__hdfs_fapl_get(H5FD_t *_file);
static void *H5FD__hdfs_fapl_copy(const void *_old_fa);
Expand All @@ -267,7 +254,7 @@ static const H5FD_class_t H5FD_hdfs_g = {
H5FD_CLASS_VERSION, /* struct version */
H5FD_HDFS_VALUE, /* value */
"hdfs", /* name */
MAXADDR, /* maxaddr */
H5FD_MAXADDR, /* maxaddr */
H5F_CLOSE_WEAK, /* fc_degree */
NULL, /* terminate */
NULL, /* sb_size */
Expand Down Expand Up @@ -840,7 +827,7 @@ H5FD__hdfs_open(const char *path, unsigned flags, hid_t fapl_id, haddr_t maxaddr
HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, NULL, "invalid file name");
if (0 == maxaddr || HADDR_UNDEF == maxaddr)
HGOTO_ERROR(H5E_ARGS, H5E_BADRANGE, NULL, "bogus maxaddr");
if (ADDR_OVERFLOW(maxaddr))
if (H5FD_ADDR_OVERFLOW(maxaddr))
HGOTO_ERROR(H5E_ARGS, H5E_OVERFLOW, NULL, "bogus maxaddr");
if (flags != H5F_ACC_RDONLY)
HGOTO_ERROR(H5E_ARGS, H5E_UNSUPPORTED, NULL, "only Read-Only access allowed");
Expand Down
29 changes: 4 additions & 25 deletions src/H5FDlog.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,27 +129,6 @@ typedef struct H5FD_log_t {
H5FD_log_fapl_t fa; /* Driver-specific file access properties */
} H5FD_log_t;

/*
* These macros check for overflow of various quantities. These macros
* assume that HDoff_t is signed and haddr_t and size_t are unsigned.
*
* ADDR_OVERFLOW: Checks whether a file address of type `haddr_t'
* is too large to be represented by the second argument
* of the file seek function.
*
* SIZE_OVERFLOW: Checks whether a buffer size of type `hsize_t' is too
* large to be represented by the `size_t' type.
*
* REGION_OVERFLOW: Checks whether an address and size pair describe data
* which can be addressed entirely by the second
* argument of the file seek function.
*/
#define MAXADDR (((haddr_t)1 << (8 * sizeof(HDoff_t) - 1)) - 1)
#define ADDR_OVERFLOW(A) (HADDR_UNDEF == (A) || ((A) & ~(haddr_t)MAXADDR))
#define SIZE_OVERFLOW(Z) ((Z) & ~(hsize_t)MAXADDR)
#define REGION_OVERFLOW(A, Z) \
(ADDR_OVERFLOW(A) || SIZE_OVERFLOW(Z) || HADDR_UNDEF == (A) + (Z) || (HDoff_t)((A) + (Z)) < (HDoff_t)(A))

/* Prototypes */
static void *H5FD__log_fapl_get(H5FD_t *file);
static void *H5FD__log_fapl_copy(const void *_old_fa);
Expand Down Expand Up @@ -177,7 +156,7 @@ static const H5FD_class_t H5FD_log_g = {
H5FD_CLASS_VERSION, /* struct version */
H5FD_LOG_VALUE, /* value */
"log", /* name */
MAXADDR, /* maxaddr */
H5FD_MAXADDR, /* maxaddr */
H5F_CLOSE_WEAK, /* fc_degree */
NULL, /* terminate */
NULL, /* sb_size */
Expand Down Expand Up @@ -450,7 +429,7 @@ H5FD__log_open(const char *name, unsigned flags, hid_t fapl_id, haddr_t maxaddr)
HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, NULL, "invalid file name");
if (0 == maxaddr || HADDR_UNDEF == maxaddr)
HGOTO_ERROR(H5E_ARGS, H5E_BADRANGE, NULL, "bogus maxaddr");
if (ADDR_OVERFLOW(maxaddr))
if (H5FD_ADDR_OVERFLOW(maxaddr))
HGOTO_ERROR(H5E_ARGS, H5E_OVERFLOW, NULL, "bogus maxaddr");

/* Initialize timers */
Expand Down Expand Up @@ -1120,7 +1099,7 @@ H5FD__log_read(H5FD_t *_file, H5FD_mem_t type, hid_t H5_ATTR_UNUSED dxpl_id, had
/* Check for overflow conditions */
if (!H5_addr_defined(addr))
HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "addr undefined, addr = %llu", (unsigned long long)addr);
if (REGION_OVERFLOW(addr, size))
if (H5FD_REGION_OVERFLOW(addr, size))
HGOTO_ERROR(H5E_ARGS, H5E_OVERFLOW, FAIL, "addr overflow, addr = %llu", (unsigned long long)addr);

/* Log the I/O information about the read */
Expand Down Expand Up @@ -1344,7 +1323,7 @@ H5FD__log_write(H5FD_t *_file, H5FD_mem_t type, hid_t H5_ATTR_UNUSED dxpl_id, ha
/* Check for overflow conditions */
if (!H5_addr_defined(addr))
HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "addr undefined, addr = %llu", (unsigned long long)addr);
if (REGION_OVERFLOW(addr, size))
if (H5FD_REGION_OVERFLOW(addr, size))
HGOTO_ERROR(H5E_ARGS, H5E_OVERFLOW, FAIL, "addr overflow, addr = %llu, size = %llu",
(unsigned long long)addr, (unsigned long long)size);

Expand Down
22 changes: 2 additions & 20 deletions src/H5FDmirror.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,24 +45,6 @@ typedef struct H5FD_mirror_t {
uint32_t xmit_i; /* Counter of transmission sent and rec'd */
} H5FD_mirror_t;

/*
* These macros check for overflow of various quantities. These macros
* assume that HDoff_t is signed and haddr_t and size_t are unsigned.
*
* ADDR_OVERFLOW: Checks whether a file address of type `haddr_t'
* is too large to be represented by the second argument
* of the file seek function.
*
* SIZE_OVERFLOW: Checks whether a buffer size of type `hsize_t' is too
* large to be represented by the `size_t' type.
*
* REGION_OVERFLOW: Checks whether an address and size pair describe data
* which can be addressed entirely by the second
* argument of the file seek function.
*/
#define MAXADDR (((haddr_t)1 << (8 * sizeof(HDoff_t) - 1)) - 1)
#define ADDR_OVERFLOW(A) (HADDR_UNDEF == (A) || ((A) & ~(haddr_t)MAXADDR))

#ifndef BSWAP_64
#define BSWAP_64(X) \
(uint64_t)((((X) & 0x00000000000000FF) << 56) | (((X) & 0x000000000000FF00) << 40) | \
Expand Down Expand Up @@ -162,7 +144,7 @@ static const H5FD_class_t H5FD_mirror_g = {
H5FD_CLASS_VERSION, /* struct version */
H5FD_MIRROR_VALUE, /* value */
"mirror", /* name */
MAXADDR, /* maxaddr */
H5FD_MAXADDR, /* maxaddr */
H5F_CLOSE_WEAK, /* fc_degree */
NULL, /* terminate */
NULL, /* sb_size */
Expand Down Expand Up @@ -1358,7 +1340,7 @@ H5FD__mirror_open(const char *name, unsigned flags, hid_t fapl_id, haddr_t maxad
HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, NULL, "filename is too long");
if (0 == maxaddr || HADDR_UNDEF == maxaddr)
HGOTO_ERROR(H5E_ARGS, H5E_BADRANGE, NULL, "bogus maxaddr");
if (ADDR_OVERFLOW(maxaddr))
if (H5FD_ADDR_OVERFLOW(maxaddr))
HGOTO_ERROR(H5E_ARGS, H5E_OVERFLOW, NULL, "bogus maxaddr");

if (H5Pget_fapl_mirror(fapl_id, &fa) == FAIL)
Expand Down
4 changes: 1 addition & 3 deletions src/H5FDonion.c
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,6 @@ typedef struct H5FD_onion_t {

H5FL_DEFINE_STATIC(H5FD_onion_t);

#define MAXADDR (((haddr_t)1 << (8 * sizeof(HDoff_t) - 1)) - 1)

#define H5FD_CTL_GET_NUM_REVISIONS 20001

/* Prototypes */
Expand All @@ -175,7 +173,7 @@ static const H5FD_class_t H5FD_onion_g = {
H5FD_CLASS_VERSION, /* struct version */
H5FD_ONION_VALUE, /* value */
"onion", /* name */
MAXADDR, /* maxaddr */
H5FD_MAXADDR, /* maxaddr */
H5F_CLOSE_WEAK, /* fc_degree */
NULL, /* terminate */
H5FD__onion_sb_size, /* sb_size */
Expand Down
24 changes: 24 additions & 0 deletions src/H5FDpkg.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,30 @@
/* Package Private Macros */
/**************************/

/* These macros check for overflow of various quantities. They are suitable
* for VFDs that are "file-like" where lseek(2), etc. is used to move around
* via HDoff_t units (i.e., most VFDs aside from the core VFD).
*
* These macros assume that HDoff_t is signed and haddr_t and size_t are unsigned.
*
* H5FD_ADDR_OVERFLOW: Checks whether a file address of type `haddr_t'
* is too large to be represented by the second argument
* of the file seek function.
*
* H5FD_SIZE_OVERFLOW: Checks whether a buffer size of type `hsize_t' is too
* large to be represented by the `size_t' type.
*
* H5FD_REGION_OVERFLOW: Checks whether an address and size pair describe data
* which can be addressed entirely by the second
* argument of the file seek function.
*/
#define H5FD_MAXADDR (((haddr_t)1 << (8 * sizeof(HDoff_t) - 1)) - 1)
#define H5FD_ADDR_OVERFLOW(A) (HADDR_UNDEF == (A) || ((A) & ~(haddr_t)H5FD_MAXADDR))
#define H5FD_SIZE_OVERFLOW(Z) ((Z) & ~(hsize_t)H5FD_MAXADDR)
#define H5FD_REGION_OVERFLOW(A, Z) \
(H5FD_ADDR_OVERFLOW(A) || H5FD_SIZE_OVERFLOW(Z) || HADDR_UNDEF == (A) + (Z) || \
(HDoff_t)((A) + (Z)) < (HDoff_t)(A))

/****************************/
/* Package Private Typedefs */
/****************************/
Expand Down
16 changes: 2 additions & 14 deletions src/H5FDros3.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,18 +129,6 @@ typedef struct H5FD_ros3_t {
#endif
} H5FD_ros3_t;

/* These macros check for overflow of various quantities. These macros
* assume that HDoff_t is signed and haddr_t and size_t are unsigned.
*
* ADDR_OVERFLOW: Checks whether a file address of type `haddr_t'
* is too large to be represented by the second argument
* of the file seek function.
* Only included if it may be used -- ROS3 VFD is enabled.
*
*/
#define MAXADDR (((haddr_t)1 << (8 * sizeof(HDoff_t) - 1)) - 1)
#define ADDR_OVERFLOW(A) (HADDR_UNDEF == (A) || ((A) & ~(haddr_t)MAXADDR))

/* Prototypes */
static void *H5FD__ros3_fapl_get(H5FD_t *_file);
static void *H5FD__ros3_fapl_copy(const void *_old_fa);
Expand Down Expand Up @@ -176,7 +164,7 @@ static const H5FD_class_t H5FD_ros3_g = {
H5FD_CLASS_VERSION, /* struct version */
H5FD_ROS3_VALUE, /* value */
"ros3", /* name */
MAXADDR, /* maxaddr */
H5FD_MAXADDR, /* maxaddr */
H5F_CLOSE_WEAK, /* fc_degree */
NULL, /* terminate */
NULL, /* sb_size */
Expand Down Expand Up @@ -725,7 +713,7 @@ H5FD__ros3_open(const char *url, unsigned flags, hid_t fapl_id, haddr_t maxaddr)
HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, NULL, "invalid file name");
if (0 == maxaddr || HADDR_UNDEF == maxaddr)
HGOTO_ERROR(H5E_ARGS, H5E_BADRANGE, NULL, "bogus maxaddr");
if (ADDR_OVERFLOW(maxaddr))
if (H5FD_ADDR_OVERFLOW(maxaddr))
HGOTO_ERROR(H5E_ARGS, H5E_OVERFLOW, NULL, "bogus maxaddr");
if (flags != H5F_ACC_RDONLY)
HGOTO_ERROR(H5E_ARGS, H5E_UNSUPPORTED, NULL, "only Read-Only access allowed");
Expand Down
Loading

0 comments on commit faec68f

Please sign in to comment.