Cherry-picking commits from master to 2.066 branch (for the minor release 2.066.1)

#3822 and #3878 are necessary for #3874.
#3849 is a trivial wrong-code bug fix.