Skip to content

Flush() is going to endless loop #1

@ikruglov

Description

@ikruglov

Hi.

Thanks for this library. It saved me lots of time. However, I'm currently facing a problem.
The following code run indefinitely and eats RAM:

package main

import (
	"bytes"

	"github.com/bluekeyes/go-gitdiff/gitdiff"
)

const (
	diff = `
diff --git a/app/controllers/seances_controller.rb b/app/controllers/seances_controller.rb
index 743d0ad..4f4d4e8 100644
--- a/app/controllers/seances_controller.rb
+++ b/app/controllers/seances_controller.rb
@@ -5,8 +5,6 @@ class SeancesController < ApplicationController
     if authorization_result.code != 200
       return render_by_status_code(code: authorization_result.code, data: authorization_result.data)
     end
-
-    render_by_status_code(code: 200, data: json)
   end

   def create
`
)

var (
	body = `class SeancesController < ApplicationController
  def index
    set_auth_operation_id_header

    if authorization_result.code != 200
      return render_by_status_code(code: authorization_result.code, data: authorization_result.data)
    end

    render_by_status_code(code: 200, data: json)
  end

  def create
    seance = Seance.new(
      movie_id: params[:movie_id],
      price: params[:price],
      datetime: params[:datetime]
    )

    if seance.save!
      render json: {
        data: {
          id: seance.id,
          type: 'seances',
          attributes: { datetime: seance.datetime, price: seance.price },
          seats: Seat.pluck(:id).map do |seat_id|
            { id: seat_id, vacant: true }
          end
        }
      }
    end
  rescue ActiveRecord::RecordInvalid => e
    render_invalid_record(message: e.message)
  end

  def destroy
    ActiveRecord::Base.transaction do
      Booking.where(seance: params[:id]).destroy_all
      Seance.find(params[:id]).destroy
    end

    render json: { data: [{ id: params[:id], type: 'seances' }] }
  end

  def json
    seats_ids = Seat.pluck(:id)

    Seance.includes(:bookings).where(movie: params[:movie_id]).order('datetime').limit(params[:max_results] || 50).map do |seance|
      booking_seats_ids = seance.bookings.pluck(:seat_id)

      {
        id: seance.id,
        price: seance.price,
        datetime: seance.datetime,
        seats: seats_ids.map do |seat_id|
          { id: seat_id, vacant: !(booking_seats_ids.include?(seat_id)) }
        end
      }
    end
  end
end
`
)

func main() {
	files, _, err := gitdiff.Parse(bytes.NewBufferString(diff))
	if err != nil {
		panic(err)
	}

	for _, file := range files {
		writer := bytes.NewBuffer(nil)
		reader := bytes.NewReader([]byte(body))
		appl := gitdiff.NewApplier(reader)
		if err := appl.ApplyFile(writer, file); err != nil {
			panic(err)
		}
	}
}

I debugged it to somewhere in Flush() operation. The internals keeps copying same lines over and over.

I would appreciate your help in debugging this.
Ivan

Metadata

Metadata

Assignees

No one assigned

    Labels

    applyingIssues related to applying patchesbugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions