Skip to content

Fix for infinite loop caused by faulty '[' handling #37

@awsdert

Description

@awsdert

The issues I fixed while resolving this are these:

  1. foo() was not checking step was valid, I added an IF_FAIL( step <= 0, SLRE_UNBALANCED_BRACKETS ); beneath it
  2. set_len() was suboptimal with a needless conditional at the end
  3. op_len() Had a sub optimal conditional
  4. get_op_len() was always adding 1 to the result of set_len() preventing it's -1 from reaching foo()

Since foo() has only 1 line of change it's not worth displaying the code below, these 3 however are right next to each and small so I decided to just display the lot in their current state.

static int op_len(const char *re) {
  return re[0] == '\\' ? (re[1] == 'x' ? 4 : 2) : 1;
}

static int set_len(const char *re, int re_len) {
  int len = 0;

  while (len < re_len) {
    len += op_len(re + len);
    if ( re[len] == ']' )
      return len + 1;
  }

  return -1;
}

static int get_op_len(const char *re, int re_len) {
  return re[0] == '[' ? set_len(re + 1, re_len - 1) : op_len(re);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions