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
diff --git a/libsparse/sparse.c b/libsparse/sparse.c
index b175860..466435f 100644
--- a/libsparse/sparse.c
+++ b/libsparse/sparse.c
@@ -179,7 +179,7 @@
}
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)
{
int ret;
int chunks;
@@ -203,11 +203,11 @@
void *priv;
unsigned int block;
unsigned int nr_blocks;
- int (*write)(void *priv, const void *data, int len, unsigned int block,
- unsigned int nr_blocks);
+ int (*write)(void *priv, const void *data, size_t len,
+ unsigned int block, unsigned int nr_blocks);
};
-static int foreach_chunk_write(void *priv, const void *data, int len)
+static int foreach_chunk_write(void *priv, const void *data, size_t len)
{
struct chunk_data *chk = priv;
@@ -215,7 +215,7 @@
}
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)
{
@@ -250,7 +250,7 @@
return ret;
}
-static int out_counter_write(void *priv, const void *data __unused, int len)
+static int out_counter_write(void *priv, const void *data __unused, size_t len)
{
int64_t *count = priv;
*count += len;