summaryrefslogtreecommitdiff
path: root/libsparse/include/sparse/sparse.h
diff options
context:
space:
mode:
authorTao Bao <tbao@google.com>2018-04-23 15:15:40 -0700
committerTao Bao <tbao@google.com>2018-04-25 10:29:22 -0700
commite18c03165b3ea86f563e104f44696d5c53f5b6a5 (patch)
tree967bd9f4d4d7fa2c57a8f194e05f47523f32c589 /libsparse/include/sparse/sparse.h
parent9249f4bc4148caef423a9cd0139d9e917fb36341 (diff)
libsparse: Use 'size_t' for the 'len' parameter in callbacks.
This CL updates the callback function signature in sparse_file_callback() and sparse_file_foreach_chunk(). Before: int sparse_file_callback( struct sparse_file *s, bool sparse, bool crc, int (*write)(void *priv, const void *data, int len), void *priv); int sparse_file_foreach_chunk( struct sparse_file *s, bool sparse, bool crc, int (*write)( void *priv, const void *data, int len, unsigned int block, unsigned int nr_blocks), void *priv); After: int sparse_file_callback( struct sparse_file *s, bool sparse, bool crc, int (*write)(void *priv, const void *data, size_t len), void *priv); int sparse_file_foreach_chunk( struct sparse_file *s, bool sparse, bool crc, int (*write)( void *priv, const void *data, size_t len, unsigned int block, unsigned int nr_blocks), void *priv); The length (i.e. 'len') comes from the size of a chunk, which could be legitimately larger than INT_MAX. Prior to this CL, callers (e.g. write_sparse_data_chunk()) were already passing unsigned int to the callbacks. When a chunk size exceeds INT_MAX, the callback would see a negative value, which could lead to undesired behavior. For example, out_counter_write(), as one of the internal callbacks in libsparse, gives a wrong sum of chunk sizes, which in turn fails the fastboot flashing when given a huge sparse image. It also defines SPARSE_CALLBACK_USES_SIZE_T that allows clients to keep their codes compatibile with both versions. Bug: 78432315 Test: `m dist` (with matching changes to all the clients) Test: Build fastboot and successfully flash a previously failing (huge) sparse image. Change-Id: Iac4bcf7b57039d08af3c57f4be00d75f6b693d93
Diffstat (limited to 'libsparse/include/sparse/sparse.h')
-rw-r--r--libsparse/include/sparse/sparse.h10
1 files changed, 8 insertions, 2 deletions
diff --git a/libsparse/include/sparse/sparse.h b/libsparse/include/sparse/sparse.h
index 356f65fd0..1b91ead5c 100644
--- a/libsparse/include/sparse/sparse.h
+++ b/libsparse/include/sparse/sparse.h
@@ -18,6 +18,7 @@
#define _LIBSPARSE_SPARSE_H_
#include <stdbool.h>
+#include <stddef.h>
#include <stdint.h>
#ifdef __cplusplus
@@ -26,6 +27,11 @@ extern "C" {
struct sparse_file;
+// The callbacks in sparse_file_callback() and sparse_file_foreach_chunk() take
+// size_t as the length type (was `int` in past). This allows clients to keep
+// their codes compatibile with both versions as needed.
+#define SPARSE_CALLBACK_USES_SIZE_T
+
/**
* sparse_file_new - create a new sparse file cookie
*
@@ -201,7 +207,7 @@ unsigned int sparse_file_block_size(struct sparse_file *s);
* Returns 0 on success, negative errno on error.
*/
int sparse_file_callback(struct sparse_file *s, bool sparse, bool crc,
- int (*write)(void *priv, const void *data, int len), void *priv);
+ int (*write)(void *priv, const void *data, size_t len), void *priv);
/**
* sparse_file_foreach_chunk - call a callback for data blocks in sparse file
@@ -218,7 +224,7 @@ int sparse_file_callback(struct sparse_file *s, bool sparse, bool crc,
* Returns 0 on success, negative errno on error.
*/
int sparse_file_foreach_chunk(struct sparse_file *s, bool sparse, bool crc,
- int (*write)(void *priv, const void *data, int len, unsigned int block,
+ int (*write)(void *priv, const void *data, size_t len, unsigned int block,
unsigned int nr_blocks),
void *priv);
/**