Edit
I forgot about the problem of CMD AutoRun registry settings. with FOR /F CMD will not disable processing of AuroRun commands by passing the /D switch to the child instance, so if any thing there outputs a single character, not only the above functions will fail, but the whole batch code will be in an unpredictable state, and most probably will be terminated, depending on the depth of the call stack and the amount of output lines by the AutoRun command(s).

I don't understand why the folks coding the CMD didn't consider this, they did this for pipes so why not for FOR /F ? And this can break many batch codes out there, for instance Dave's getPID routine. Surely the practice of putting something in AutoRun is not common, but peaple do crazy things when they made aware of some feature, without knowing the possible consequences of doing so.

OK, complaining doesn't solve any thing, this is the replacement code, fortunately, for this case the workaround is simple.

I don't see any buffer overflow, I believe it's just an exhausted buffer, nothing will be written beyond. If we can cause buffer overflow this easy, then the side effects of getting CR this way would be the least our worries.

But to be sure I've traced the relevant parts of cmd code in debugger, and I couldn't find any evidence of buffer overflow being occured. In this particular case the only place that is susceptible to cause overflow is where 'echo ' tries to append CRLF to the passed argument.