Skip to content

Bad Command alignment #291

@Jan200101

Description

@Jan200101

lite/src/rencache.c

Lines 95 to 102 in 38bd9b3

static bool next_command(Command **prev) {
if (*prev == NULL) {
*prev = (Command*) command_buf;
} else {
*prev = (Command*) (((char*) *prev) + (*prev)->size);
}
return *prev != ((Command*) (command_buf + command_buf_idx));
}

This function moves prev size along which can and will cause unalignment and potential errors.

On ARM devices this causes a SIGBUS.

The people who helped me figure this out suggested this fix

static Command* push_command(int type, int size) {
+  size_t alignment = alignof(max_align_t) - 1;
+  size = (size + alignment) & ~alignment; // forward align to 4
  Command *cmd = (Command*) (command_buf + command_buf_idx);
  int n = command_buf_idx + size;
  if (n > COMMAND_BUF_SIZE) {
    fprintf(stderr, "Warning: (" __FILE__ "): exhausted command buffer\n");
    return NULL;
  }
  command_buf_idx = n;
  memset(cmd, 0, sizeof(Command));
  cmd->type = type;
  cmd->size = size;
  return cmd;
}

But alignof and max_align_t have been introduced with C11 so it might be of interest to replace the alignof with a static alignment of either 4 or 16 to be safe

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions