This appears to be a duplicate of 
https://debbugs.gnu.org/cgi/bugreport.cgi?bug=23570

I applied a patch mentioned in that thread 
(commit 99848b3 in master) and that fixes the issue
in all the cases that were causing problems here.

The other thread resolved to leave the fix for 25.2, 
so I guess this bug can be closed. 

Sorry for the duplication.

Nick