While I don't claim my scanning is perfect, I haven't detected any real problems in your code, apart from a nit: str[0] should be initialised to zero, not NULL [although I realise some examples from Microsoft use NULL - goes to show their programmers are human and imperfect too].

My guess (with about 95% confidence) is that your real problem is in some other code executed before your function is even called. One dubious joy associated with pointer molestation, for example, is that symptoms do not always appear immediately, so some random area of memory is overwritten. That can cause the phenomena you describe (adding an unrelated variable causing the symptom to disappear for no apparent reason) because defining an additional variable affects the layout of memory used by your function and your program, therefore can change the observable effect of writing to memory you shouldn't.