001-sed-fix-handling-of-symlinks-pointing-to-path-with-1.patch 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. From 8f600f2df293d539e9e9137f6f82faa1633b97c1 Mon Sep 17 00:00:00 2001
  2. From: Paul Eggert <eggert@cs.ucla.edu>
  3. Date: Sat, 17 Dec 2022 20:56:29 -0800
  4. Subject: [PATCH] sed: fix symlink bufsize readlink check
  5. Problem reported by Hauke Mehrtens.
  6. * sed/utils.c (follow_symlink): Fix typo when checking size of
  7. second and later symlink, when that symlink is so large that it
  8. does not fit into the buffer. Although the bug is not a buffer
  9. overflow, it does cause sed to mishandle the symlink.
  10. * testsuite/follow-symlinks.sh: Test for the bug.
  11. ---
  12. sed/utils.c | 2 +-
  13. testsuite/follow-symlinks.sh | 13 +++++++++++++
  14. 3 files changed, 18 insertions(+), 1 deletion(-)
  15. --- a/sed/utils.c
  16. +++ b/sed/utils.c
  17. @@ -345,7 +345,7 @@ follow_symlink (const char *fname)
  18. while ((linklen = (buf_used < buf_size
  19. ? readlink (fn, buf + buf_used, buf_size - buf_used)
  20. : 0))
  21. - == buf_size)
  22. + == buf_size - buf_used)
  23. {
  24. buf = xpalloc (buf, &buf_size, 1, SSIZE_IDX_MAX, 1);
  25. if (num_links)
  26. --- a/testsuite/follow-symlinks.sh
  27. +++ b/testsuite/follow-symlinks.sh
  28. @@ -73,4 +73,17 @@ compare_ exp-la-abs out-la-abs || fail=1
  29. ln -s la-loop la-loop || framework_failure_
  30. sed --follow-symlinks -i s/a/b/ la-loop && fail=1
  31. +# symlink of length 128
  32. +long=d/
  33. +for i in 2 3 4 5 6 7; do
  34. + long=$long$long
  35. +done
  36. +dir=${long%/d/}
  37. +file=$dir/xx
  38. +mkdir -p $dir &&
  39. +echo x >$file &&
  40. +ln -s $file yy &&
  41. +ln -s yy xx || framework_failure_
  42. +sed -i --follow-symlinks s/x/y/ xx || fail=1
  43. +
  44. Exit $fail