go-search-replace icon indicating copy to clipboard operation
go-search-replace copied to clipboard

Replace corrupts serialized data if the search and replace are of different length

Open sjinks opened this issue 3 years ago • 5 comments

To reproduce:

php -r 'echo serialize("aaaaabbbbbbbbbbaaaaa"), PHP_EOL;' > test.txt
cat test.txt | ./go-search-replace bbbbbbbbbb ccccc
cat test.txt | ./go-search-replace bbbbbbbbbb ccccccccccccccc

Expected result:

s:15:"aaaaacccccaaaaa";
s:25:"aaaaacccccccccccccccaaaaa";

Actual result:

s:20:"aaaaacccccaaaaa";
s:20:"aaaaacccccccccccccccaaaaa";

sjinks avatar Dec 10 '22 21:12 sjinks

This is because go-search-replace expects the quote marks to be escaped. Whether that's a bug or not I don't know.

$ php -r 'echo addslashes( serialize("aaaaabbbbbbbbbbaaaaa") ), PHP_EOL;' > test.txt
$ cat test.txt                                                                    
s:20:\"aaaaabbbbbbbbbbaaaaa\";

$ cat test.txt | go-search-replace bbbbbbbbbb ccccc                               
s:15:\"aaaaacccccaaaaa\";

$ cat test.txt | go-search-replace bbbbbbbbbb ccccccccccccccc                     
s:25:\"aaaaacccccccccccccccaaaaa\";

trepmal avatar Jan 27 '23 05:01 trepmal

But then it does not work on statements like

INSERT INTO `wp_options` (option_name, option_value) VALUES ('test', 's:15:"aaaaacccccaaaaa";');

sjinks avatar Jan 27 '23 10:01 sjinks

Double quotes inside single quotes do not have to be escaped.

sjinks avatar Jan 27 '23 10:01 sjinks

I think WP always escapes the quotes, but if you're seeing otherwise it should be fine to change the regex

https://github.com/Automattic/go-search-replace/blob/master/search-replace.go#L17-L18

joshbetz avatar Jan 27 '23 14:01 joshbetz

The reason it behaves like that is that mysqldump will escape double quotes unnecessarily, therefore it keeps this behavior, since unescaped double quotes indicate other data, that should generally not be replaced by go-search-replace

kkmuffme avatar Sep 29 '25 20:09 kkmuffme