Skip to content

Commit bf1028a

Browse files
GustavoARSilvasmfrench
authored andcommitted
cifs: misc: Use array_size() in if-statement controlling expression
Use array_size() instead of the open-coded version in the controlling expression of the if statement. Also, while there, use the preferred form for passing a size of a struct. The alternative form where struct name is spelled out hurts readability and introduces an opportunity for a bug when the pointer variable type is changed but the corresponding sizeof that is passed as argument is not. This issue was found with the help of Coccinelle and, audited and fixed manually. Addresses-KSPP-ID: KSPP#83 Signed-off-by: Gustavo A. R. Silva <[email protected]> Signed-off-by: Steve French <[email protected]> Reviewed-by: Aurelien Aptel <[email protected]> Reviewed-by: Kees Cook <[email protected]> Signed-off-by: Steve French <[email protected]>
1 parent 5618303 commit bf1028a

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

fs/cifs/misc.c

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -844,28 +844,26 @@ setup_aio_ctx_iter(struct cifs_aio_ctx *ctx, struct iov_iter *iter, int rw)
844844
struct bio_vec *bv = NULL;
845845

846846
if (iov_iter_is_kvec(iter)) {
847-
memcpy(&ctx->iter, iter, sizeof(struct iov_iter));
847+
memcpy(&ctx->iter, iter, sizeof(*iter));
848848
ctx->len = count;
849849
iov_iter_advance(iter, count);
850850
return 0;
851851
}
852852

853-
if (max_pages * sizeof(struct bio_vec) <= CIFS_AIO_KMALLOC_LIMIT)
854-
bv = kmalloc_array(max_pages, sizeof(struct bio_vec),
855-
GFP_KERNEL);
853+
if (array_size(max_pages, sizeof(*bv)) <= CIFS_AIO_KMALLOC_LIMIT)
854+
bv = kmalloc_array(max_pages, sizeof(*bv), GFP_KERNEL);
856855

857856
if (!bv) {
858-
bv = vmalloc(array_size(max_pages, sizeof(struct bio_vec)));
857+
bv = vmalloc(array_size(max_pages, sizeof(*bv)));
859858
if (!bv)
860859
return -ENOMEM;
861860
}
862861

863-
if (max_pages * sizeof(struct page *) <= CIFS_AIO_KMALLOC_LIMIT)
864-
pages = kmalloc_array(max_pages, sizeof(struct page *),
865-
GFP_KERNEL);
862+
if (array_size(max_pages, sizeof(*pages)) <= CIFS_AIO_KMALLOC_LIMIT)
863+
pages = kmalloc_array(max_pages, sizeof(*pages), GFP_KERNEL);
866864

867865
if (!pages) {
868-
pages = vmalloc(array_size(max_pages, sizeof(struct page *)));
866+
pages = vmalloc(array_size(max_pages, sizeof(*pages)));
869867
if (!pages) {
870868
kvfree(bv);
871869
return -ENOMEM;

0 commit comments

Comments
 (0)