Skip to content

Commit

Permalink
block: sed-opal: unify space check in add_token_*
Browse files Browse the repository at this point in the history
All add_token_* functions have a common set of conditions that have to
be checked. Use a common function for those checks in order to avoid
different behaviour as well as code duplication.

Acked-by: Jon Derrick <[email protected]>
Reviewed-by: Christoph Hellwig <[email protected]>
Reviewed-by: Scott Bauer <[email protected]>
Co-authored-by: David Kozub <[email protected]>
Signed-off-by: Jonas Rabenstein <[email protected]>
Signed-off-by: David Kozub <[email protected]>
Signed-off-by: Jens Axboe <[email protected]>
  • Loading branch information
2 people authored and axboe committed Apr 6, 2019
1 parent 1b6b75b commit e2821a5
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions block/sed-opal.c
Original file line number Diff line number Diff line change
Expand Up @@ -510,15 +510,24 @@ static int opal_discovery0(struct opal_dev *dev, void *data)
return opal_discovery0_end(dev);
}

static void add_token_u8(int *err, struct opal_dev *cmd, u8 tok)
static bool can_add(int *err, struct opal_dev *cmd, size_t len)
{
if (*err)
return;
if (cmd->pos >= IO_BUFFER_LENGTH - 1) {
pr_debug("Error adding u8: end of buffer.\n");
return false;

if (len > IO_BUFFER_LENGTH || cmd->pos > IO_BUFFER_LENGTH - len) {
pr_debug("Error adding %zu bytes: end of buffer.\n", len);
*err = -ERANGE;
return;
return false;
}

return true;
}

static void add_token_u8(int *err, struct opal_dev *cmd, u8 tok)
{
if (!can_add(err, cmd, 1))
return;
cmd->cmd[cmd->pos++] = tok;
}

Expand Down Expand Up @@ -562,9 +571,8 @@ static void add_token_u64(int *err, struct opal_dev *cmd, u64 number)
msb = fls64(number);
len = DIV_ROUND_UP(msb, 8);

if (cmd->pos >= IO_BUFFER_LENGTH - len - 1) {
if (!can_add(err, cmd, len + 1)) {
pr_debug("Error adding u64: end of buffer.\n");
*err = -ERANGE;
return;
}
add_short_atom_header(cmd, false, false, len);
Expand All @@ -586,9 +594,8 @@ static void add_token_bytestring(int *err, struct opal_dev *cmd,
is_short_atom = false;
}

if (len >= IO_BUFFER_LENGTH - cmd->pos - header_len) {
if (!can_add(err, cmd, header_len + len)) {
pr_debug("Error adding bytestring: end of buffer.\n");
*err = -ERANGE;
return;
}

Expand Down

0 comments on commit e2821a5

Please sign in to comment.