Skip to content

Commit 0e71666

Browse files
GustavoARSilvatorvalds
authored andcommitted
ocfs2/dlm: use struct_size() helper
One of the more common cases of allocation size calculations is finding the size of a structure that has a zero-sized array at the end, along with memory for some number of elements for that array. For example: struct dlm_migratable_lockres { ... struct dlm_migratable_lock ml[0]; // 16 bytes each, begins at byte 112 }; Make use of the struct_size() helper instead of an open-coded version in order to avoid any potential type mistakes. So, replace the following form: sizeof(struct dlm_migratable_lockres) + (mres->num_locks * sizeof(struct dlm_migratable_lock)) with: struct_size(mres, ml, mres->num_locks) Notice that, in this case, variable sz is not necessary, hence it is removed. This code was detected with the help of Coccinelle. Link: http://lkml.kernel.org/r/20190605204926.GA24467@embeddedor Signed-off-by: Gustavo A. R. Silva <[email protected]> Reviewed-by: Joseph Qi <[email protected]> Cc: Mark Fasheh <[email protected]> Cc: Joel Becker <[email protected]> Cc: Junxiao Bi <[email protected]> Cc: Changwei Ge <[email protected]> Cc: Gang He <[email protected]> Cc: Jun Piao <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent e926d8a commit 0e71666

File tree

1 file changed

+3
-5
lines changed

1 file changed

+3
-5
lines changed

fs/ocfs2/dlm/dlmrecovery.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,17 +1109,14 @@ static int dlm_send_mig_lockres_msg(struct dlm_ctxt *dlm,
11091109
{
11101110
u64 mig_cookie = be64_to_cpu(mres->mig_cookie);
11111111
int mres_total_locks = be32_to_cpu(mres->total_locks);
1112-
int sz, ret = 0, status = 0;
1112+
int ret = 0, status = 0;
11131113
u8 orig_flags = mres->flags,
11141114
orig_master = mres->master;
11151115

11161116
BUG_ON(mres->num_locks > DLM_MAX_MIGRATABLE_LOCKS);
11171117
if (!mres->num_locks)
11181118
return 0;
11191119

1120-
sz = sizeof(struct dlm_migratable_lockres) +
1121-
(mres->num_locks * sizeof(struct dlm_migratable_lock));
1122-
11231120
/* add an all-done flag if we reached the last lock */
11241121
orig_flags = mres->flags;
11251122
BUG_ON(total_locks > mres_total_locks);
@@ -1133,7 +1130,8 @@ static int dlm_send_mig_lockres_msg(struct dlm_ctxt *dlm,
11331130

11341131
/* send it */
11351132
ret = o2net_send_message(DLM_MIG_LOCKRES_MSG, dlm->key, mres,
1136-
sz, send_to, &status);
1133+
struct_size(mres, ml, mres->num_locks),
1134+
send_to, &status);
11371135
if (ret < 0) {
11381136
/* XXX: negative status is not handled.
11391137
* this will end up killing this node. */

0 commit comments

Comments
 (0)