use a dynamic buffer in strdup_and_subst_obj0
As discussed on !1509 (merged), this rewrites some manual calculation and allocation code to use a dynamically expanding buffer. This simplifies the code and makes it more robust against bugs like that fixed in fbefeb31.
I'll merge on 2020-08-21 if there are no objections.