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

Fix incorrect std::string_view usage #1750

Open
wants to merge 21 commits into
base: Piers
Choose a base branch
from

Conversation

neo1973
Copy link
Member

@neo1973 neo1973 commented Jan 2, 2025

Description

In many places it was assumed that std::string_view::data() would return a null terminated string that only contains the slice the view is pointing at. This is in general not true.

Motivation and context

I became aware of this general issue when investigating #1748. This PR probably solved that issue but I'm not certain.

How has this been tested?

Test suite runs, YouTube works.

Types of change

  • Bug fix (non-breaking change which fixes an issue)
  • Clean up (non-breaking change which removes non-working, unmaintained functionality)
  • Improvement (non-breaking change which improves existing functionality)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that will cause existing functionality to change)
  • Cosmetic change (non-breaking change that doesn't touch code)
  • None of the above (please explain below)

Checklist:

  • I have read the Contributing document
  • My code follows the Code Guidelines of this project
  • My change requires a change to the Wiki documentation
  • I have updated the documentation accordingly

neo1973 added 21 commits January 2, 2025 13:28
`std::string_view` is not null terminated, therefore `cbegin()`/`cend()`
iterator pairs need to be used.
Those functions are thin wrappers around C functions that can't handle non null
terminated strings.
@neo1973 neo1973 added Type: Fix non-breaking change which fixes an issue v22 Piers labels Jan 2, 2025
@CastagnaIT
Copy link
Collaborator

in many places the use of string_view is intentional
so i dont agree in full this pr

all logs changes are confusing due to additional length input

LOG::LogF(LOGWARNING, "Skipped AdaptationSet with id: \"%.*s\", has no representations.",
              static_cast<int>(adpSet->GetId().length()), adpSet->GetId().data());

fwik all strings manged on ISA are null terminated, if it is missing there is to understand why

@neo1973
Copy link
Member Author

neo1973 commented Jan 2, 2025

all logs changes are confusing due to additional length input

Probably time to migrate to std::format?

fwik all strings manged on ISA are null terminated, if it is missing there is to understand why

This might very well be true, but someone who doesn't know the ins and outs of inputstream.adaptive has to check every time a function accepts a std::string_view if it in reality expects a c-style string.

See e.g.:

width = STRING::ToInt32(val.substr(0, pos));

This has been special cased in StringUtils:
int UTILS::STRING::ToInt32(std::string_view str, int fallback /* = 0 */)
{
int result = fallback;
std::from_chars(str.data(), str.data() + str.size(), result);
return result;
}

Do the same for UTILS::STRING::ToUint32 and it won't work. And that's the main problem. You can't look at a function and be certain that is does the correct thing even if you use it according to its contract.

@neo1973 neo1973 mentioned this pull request Jan 2, 2025
11 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Fix non-breaking change which fixes an issue v22 Piers
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants