From c36adb98aa62a0f22f6ed290589cd2faf108abc4 Mon Sep 17 00:00:00 2001 From: Stan Hu Date: Tue, 11 Aug 2015 18:39:27 -0700 Subject: Fix bug where backslashes in inline diffs could be dropped Closes #2253 --- CHANGELOG | 1 + lib/gitlab/inline_diff.rb | 7 ++++-- spec/lib/gitlab/diff/inline_diff_spec.rb | 39 ++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 spec/lib/gitlab/diff/inline_diff_spec.rb diff --git a/CHANGELOG b/CHANGELOG index e14255ee9b2..b6f9098a991 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,7 @@ Please view this file on the master branch, on stable branches it's out of date. v 7.14.0 (unreleased) + - Fix bug where backslashes in inline diffs could be dropped (Stan Hu) - Disable turbolinks when linking to Bitbucket import status (Stan Hu) - Fix broken code import and display error messages if something went wrong with creating project (Stan Hu) - Fix corrupted binary files when using API files endpoint (Stan Hu) diff --git a/lib/gitlab/inline_diff.rb b/lib/gitlab/inline_diff.rb index 3517ecdf5cf..99e7b529ba9 100644 --- a/lib/gitlab/inline_diff.rb +++ b/lib/gitlab/inline_diff.rb @@ -46,8 +46,11 @@ module Gitlab end last_the_same_symbols += 1 last_token = first_line[last_the_same_symbols..-1] - diff_arr[index+1].sub!(/#{Regexp.escape(last_token)}$/, FINISH + last_token) - diff_arr[index+2].sub!(/#{Regexp.escape(last_token)}$/, FINISH + last_token) + # This is tricky: escape backslashes so that `sub` doesn't interpret them + # as backreferences. Regexp.escape does NOT do the right thing. + replace_token = FINISH + last_token.gsub(/\\/, '\&\&') + diff_arr[index+1].sub!(/#{Regexp.escape(last_token)}$/, replace_token) + diff_arr[index+2].sub!(/#{Regexp.escape(last_token)}$/, replace_token) end diff_arr end diff --git a/spec/lib/gitlab/diff/inline_diff_spec.rb b/spec/lib/gitlab/diff/inline_diff_spec.rb new file mode 100644 index 00000000000..2e0a05088cc --- /dev/null +++ b/spec/lib/gitlab/diff/inline_diff_spec.rb @@ -0,0 +1,39 @@ +require 'spec_helper' + +describe Gitlab::InlineDiff do + describe '#processing' do + let(:diff) do + <