This should only match when firstPattern == -1.  That is, we want to find the *last* Bug: or Issue: line and insert the Change-Id immediately after it.  Here you are going to insert it after the first.  Correct output would be:

  Bug: 42
  Bug: 53
  Bug: 59
  Change-Id: I....
  Signed-off-by: Shawn ...

That's why I do this as two loops in the canonical hook.  We loop backwards to find the end of the footers, and then loop forwards to find the Change-Id insertion point.  Given that its only a couple of indexes, walking backwards then forwards in the data is very inexpensive.