0

Is this really a potential crash ?

Hi I have seen code below in production code: https://code.sololearn.com/ccwwhbcn1Spu I am amazed to see this. Is it only me who think it is bad or is it really bad? What if ws as assigned to Chinese characters like below? These can't be hold by char buffer...right? //wstring ws(L"你好吗"); Second point is why to use %ls on char buff.... What does %ls really means? Another thing is like count in sprintf... Isn't it should be one less like _MAX_PATH - 1 for argument of sprintf? In short, I believe that this code is powerhouse of potential crash. Is it true?

17th Jul 2022, 9:19 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
1 Respuesta
+ 1
Yes the narrowing conversion from wchar to char might be a real problem. But snprintf will simply return a negative number if you for example try to write chinese characters into the char array. In any real application you really would want to check for this and/or make sure the input is always in ascii range. The %ls is a long string. meaning a unicode string instead of the usual ascii string. That is correct. Also the max_path should be fine since every char array is implicitly padded with an 0 byte at the end. So it should end up correctly from what I get.
19th Jul 2022, 8:48 AM
Erarnitox
Erarnitox - avatar