Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CDeviceUtils::ConvertToString() is error-prone #439

Open
marktsuchida opened this issue Jan 25, 2024 · 0 comments
Open

CDeviceUtils::ConvertToString() is error-prone #439

marktsuchida opened this issue Jan 25, 2024 · 0 comments

Comments

@marktsuchida
Copy link
Member

It uses a common static buffer, so it is (1) not thread-safe and (2) error-prone even when used in only one thread.

For example, what this code will print is up to the whim of the compiler:

LogMessage(string("Trying to perform a rtu connection via ") + conuri.hostandaddress + " baud: " + CDeviceUtils::ConvertToString(baud) + " parity: " + parityChars + " data_bit: " + CDeviceUtils::ConvertToString(data_bit) + " stop_bit: " + CDeviceUtils::ConvertToString(stop_bit));

Some good news is that we now have the convenient std::to_string(). We should be able to replace CDeviceUtils::ConvertToString(x) with std::to_string(x).c_str() in most cases. However,

  • The formatting will not match in the case of double x
  • Special case is needed for bool x if maintaining exact behavior
  • We should ensure that there are no performance issues when used in the critical path of camera frame metadata generation (probably okay because formatted numbers usually fit in std::string's small-buffer optimization, meaning no allocation happens -- but something to be aware of)

In any case the solution is not quite trivial, and I don't want to add a deprecation warning for this (too many uses), so recording this issue for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant