crypto: Make block callbacks return 0 on success

They currently return the value of their headerlen/buflen parameter on
success. Returning 0 instead makes it clear that short reads/writes are
not possible.

Signed-off-by: Alberto Faria <afaria@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
Message-Id: <20220609152744.3891847-5-afaria@redhat.com>
Reviewed-by: Hanna Reitz <hreitz@redhat.com>
Signed-off-by: Hanna Reitz <hreitz@redhat.com>
This commit is contained in:
Alberto Faria 2022-06-09 16:27:38 +01:00 committed by Hanna Reitz
parent 353a5d84b2
commit 757dda54b4
6 changed files with 79 additions and 79 deletions

View File

@ -55,12 +55,12 @@ static int block_crypto_probe_generic(QCryptoBlockFormat format,
}
static ssize_t block_crypto_read_func(QCryptoBlock *block,
size_t offset,
uint8_t *buf,
size_t buflen,
void *opaque,
Error **errp)
static int block_crypto_read_func(QCryptoBlock *block,
size_t offset,
uint8_t *buf,
size_t buflen,
void *opaque,
Error **errp)
{
BlockDriverState *bs = opaque;
ssize_t ret;
@ -70,15 +70,15 @@ static ssize_t block_crypto_read_func(QCryptoBlock *block,
error_setg_errno(errp, -ret, "Could not read encryption header");
return ret;
}
return buflen;
return 0;
}
static ssize_t block_crypto_write_func(QCryptoBlock *block,
size_t offset,
const uint8_t *buf,
size_t buflen,
void *opaque,
Error **errp)
static int block_crypto_write_func(QCryptoBlock *block,
size_t offset,
const uint8_t *buf,
size_t buflen,
void *opaque,
Error **errp)
{
BlockDriverState *bs = opaque;
ssize_t ret;
@ -88,7 +88,7 @@ static ssize_t block_crypto_write_func(QCryptoBlock *block,
error_setg_errno(errp, -ret, "Could not write encryption header");
return ret;
}
return buflen;
return 0;
}
@ -99,12 +99,12 @@ struct BlockCryptoCreateData {
};
static ssize_t block_crypto_create_write_func(QCryptoBlock *block,
size_t offset,
const uint8_t *buf,
size_t buflen,
void *opaque,
Error **errp)
static int block_crypto_create_write_func(QCryptoBlock *block,
size_t offset,
const uint8_t *buf,
size_t buflen,
void *opaque,
Error **errp)
{
struct BlockCryptoCreateData *data = opaque;
ssize_t ret;
@ -114,13 +114,13 @@ static ssize_t block_crypto_create_write_func(QCryptoBlock *block,
error_setg_errno(errp, -ret, "Could not write encryption header");
return ret;
}
return ret;
return 0;
}
static ssize_t block_crypto_create_init_func(QCryptoBlock *block,
size_t headerlen,
void *opaque,
Error **errp)
static int block_crypto_create_init_func(QCryptoBlock *block,
size_t headerlen,
void *opaque,
Error **errp)
{
struct BlockCryptoCreateData *data = opaque;
Error *local_error = NULL;
@ -139,7 +139,7 @@ static ssize_t block_crypto_create_init_func(QCryptoBlock *block,
data->prealloc, 0, &local_error);
if (ret >= 0) {
return ret;
return 0;
}
error:

View File

@ -94,9 +94,9 @@ static int qcow2_probe(const uint8_t *buf, int buf_size, const char *filename)
}
static ssize_t qcow2_crypto_hdr_read_func(QCryptoBlock *block, size_t offset,
uint8_t *buf, size_t buflen,
void *opaque, Error **errp)
static int qcow2_crypto_hdr_read_func(QCryptoBlock *block, size_t offset,
uint8_t *buf, size_t buflen,
void *opaque, Error **errp)
{
BlockDriverState *bs = opaque;
BDRVQcow2State *s = bs->opaque;
@ -113,12 +113,12 @@ static ssize_t qcow2_crypto_hdr_read_func(QCryptoBlock *block, size_t offset,
error_setg_errno(errp, -ret, "Could not read encryption header");
return -1;
}
return buflen;
return 0;
}
static ssize_t qcow2_crypto_hdr_init_func(QCryptoBlock *block, size_t headerlen,
void *opaque, Error **errp)
static int qcow2_crypto_hdr_init_func(QCryptoBlock *block, size_t headerlen,
void *opaque, Error **errp)
{
BlockDriverState *bs = opaque;
BDRVQcow2State *s = bs->opaque;
@ -151,13 +151,13 @@ static ssize_t qcow2_crypto_hdr_init_func(QCryptoBlock *block, size_t headerlen,
return -1;
}
return ret;
return 0;
}
static ssize_t qcow2_crypto_hdr_write_func(QCryptoBlock *block, size_t offset,
const uint8_t *buf, size_t buflen,
void *opaque, Error **errp)
static int qcow2_crypto_hdr_write_func(QCryptoBlock *block, size_t offset,
const uint8_t *buf, size_t buflen,
void *opaque, Error **errp)
{
BlockDriverState *bs = opaque;
BDRVQcow2State *s = bs->opaque;
@ -174,7 +174,7 @@ static ssize_t qcow2_crypto_hdr_write_func(QCryptoBlock *block, size_t offset,
error_setg_errno(errp, -ret, "Could not read encryption header");
return -1;
}
return buflen;
return 0;
}
static QDict*

View File

@ -495,7 +495,7 @@ qcrypto_block_luks_load_header(QCryptoBlock *block,
void *opaque,
Error **errp)
{
ssize_t rv;
int rv;
size_t i;
QCryptoBlockLUKS *luks = block->opaque;
@ -856,7 +856,7 @@ qcrypto_block_luks_store_key(QCryptoBlock *block,
QCRYPTO_BLOCK_LUKS_SECTOR_SIZE,
splitkey, splitkeylen,
opaque,
errp) != splitkeylen) {
errp) < 0) {
goto cleanup;
}
@ -903,7 +903,7 @@ qcrypto_block_luks_load_key(QCryptoBlock *block,
g_autofree uint8_t *splitkey = NULL;
size_t splitkeylen;
g_autofree uint8_t *possiblekey = NULL;
ssize_t rv;
int rv;
g_autoptr(QCryptoCipher) cipher = NULL;
uint8_t keydigest[QCRYPTO_BLOCK_LUKS_DIGEST_LEN];
g_autoptr(QCryptoIVGen) ivgen = NULL;
@ -1193,7 +1193,7 @@ qcrypto_block_luks_erase_key(QCryptoBlock *block,
garbagesplitkey,
splitkeylen,
opaque,
&local_err) != splitkeylen) {
&local_err) < 0) {
error_propagate(errp, local_err);
return -1;
}

View File

@ -115,7 +115,7 @@ QCryptoBlock *qcrypto_block_create(QCryptoBlockCreateOptions *options,
}
static ssize_t qcrypto_block_headerlen_hdr_init_func(QCryptoBlock *block,
static int qcrypto_block_headerlen_hdr_init_func(QCryptoBlock *block,
size_t headerlen, void *opaque, Error **errp)
{
size_t *headerlenp = opaque;
@ -126,12 +126,12 @@ static ssize_t qcrypto_block_headerlen_hdr_init_func(QCryptoBlock *block,
}
static ssize_t qcrypto_block_headerlen_hdr_write_func(QCryptoBlock *block,
static int qcrypto_block_headerlen_hdr_write_func(QCryptoBlock *block,
size_t offset, const uint8_t *buf, size_t buflen,
void *opaque, Error **errp)
{
/* Discard the bytes, we're not actually writing to an image */
return buflen;
return 0;
}

View File

@ -29,24 +29,24 @@ typedef struct QCryptoBlock QCryptoBlock;
/* See also QCryptoBlockFormat, QCryptoBlockCreateOptions
* and QCryptoBlockOpenOptions in qapi/crypto.json */
typedef ssize_t (*QCryptoBlockReadFunc)(QCryptoBlock *block,
size_t offset,
uint8_t *buf,
size_t buflen,
void *opaque,
Error **errp);
typedef int (*QCryptoBlockReadFunc)(QCryptoBlock *block,
size_t offset,
uint8_t *buf,
size_t buflen,
void *opaque,
Error **errp);
typedef ssize_t (*QCryptoBlockInitFunc)(QCryptoBlock *block,
size_t headerlen,
void *opaque,
Error **errp);
typedef int (*QCryptoBlockInitFunc)(QCryptoBlock *block,
size_t headerlen,
void *opaque,
Error **errp);
typedef ssize_t (*QCryptoBlockWriteFunc)(QCryptoBlock *block,
size_t offset,
const uint8_t *buf,
size_t buflen,
void *opaque,
Error **errp);
typedef int (*QCryptoBlockWriteFunc)(QCryptoBlock *block,
size_t offset,
const uint8_t *buf,
size_t buflen,
void *opaque,
Error **errp);
/**
* qcrypto_block_has_format:

View File

@ -188,12 +188,12 @@ static struct QCryptoBlockTestData {
};
static ssize_t test_block_read_func(QCryptoBlock *block,
size_t offset,
uint8_t *buf,
size_t buflen,
void *opaque,
Error **errp)
static int test_block_read_func(QCryptoBlock *block,
size_t offset,
uint8_t *buf,
size_t buflen,
void *opaque,
Error **errp)
{
Buffer *header = opaque;
@ -201,14 +201,14 @@ static ssize_t test_block_read_func(QCryptoBlock *block,
memcpy(buf, header->buffer + offset, buflen);
return buflen;
return 0;
}
static ssize_t test_block_init_func(QCryptoBlock *block,
size_t headerlen,
void *opaque,
Error **errp)
static int test_block_init_func(QCryptoBlock *block,
size_t headerlen,
void *opaque,
Error **errp)
{
Buffer *header = opaque;
@ -216,16 +216,16 @@ static ssize_t test_block_init_func(QCryptoBlock *block,
buffer_reserve(header, headerlen);
return headerlen;
return 0;
}
static ssize_t test_block_write_func(QCryptoBlock *block,
size_t offset,
const uint8_t *buf,
size_t buflen,
void *opaque,
Error **errp)
static int test_block_write_func(QCryptoBlock *block,
size_t offset,
const uint8_t *buf,
size_t buflen,
void *opaque,
Error **errp)
{
Buffer *header = opaque;
@ -234,7 +234,7 @@ static ssize_t test_block_write_func(QCryptoBlock *block,
memcpy(header->buffer + offset, buf, buflen);
header->offset = offset + buflen;
return buflen;
return 0;
}