bbf: Cleanup and improve the code
- Fix all errors catched by cppcheck threadsafety (cppcheck --error-exitcode=0 --addon=threadsafety bbf)
- Fix some errors catched by cppcheck cert (cppcheck --error-exitcode=0 --addon=cert bbf)
- Add new str-protected macros instead of using str functions directly to avoid crashes