- 
                Notifications
    
You must be signed in to change notification settings  - Fork 144
 
Avoid assuming that "std::string_view" is nullterminated. Generally it is really not. #538
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
base: main
Are you sure you want to change the base?
Conversation
          Summary of ChangesHello @ActuallyaDeviloper, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request resolves critical issues across multiple backend serialization/deserialization libraries (Avro, Cap'n Proto, JSON, FlexBuf, YAML, XML) where  Highlights
 Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either  
 Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a  Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
  | 
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request does a good job of addressing the incorrect assumption that std::string_view is null-terminated across various serialization backends. The changes correctly use size-aware constructors or create temporary std::string objects to ensure null-termination where C-style strings are required. This improves the robustness and correctness of the library.
However, I've found a few issues that need to be addressed:
- In 
include/rfl/capnproto/Writer.hpp, there are a couple of bugs: one is a copy-paste error that uses_nameinstead of_var, and another is a change that will cause a compilation error. - In 
src/rfl/flexbuf/Writer.cpp, one of the function calls was missed in the refactoring and still incorrectly uses.data()on astring_view. - A UTF-8 BOM was added to 
include/rfl/json/Reader.hpp, which is generally discouraged and should be removed. 
Once these issues are fixed, this will be a great improvement to the library.
86c4234    to
    4693ccd      
    Compare
  
    | 
           @ActuallyaDeviloper, in practice, the string are always derived from here: https://github.com/getml/reflect-cpp/blob/main/include/rfl/internal/StringLiteral.hpp So actually, it is OK to assume that they are null-terminated. But where you can increase safety without generating unnecessary copies, I think it is a good idea.  | 
    
| 
           @ActuallyaDeviloper , some of the tests are failing to compile. Could you have a look?  | 
    
| 
           @ActuallyaDeviloper , out of curiosity: For which format did you want to write your own parser? Would you be willing to contribute it to the library?  | 
    
| 
           Hm, when I implemented my own parser this was not necessarily true anymore, as I was pumping my own "string_view" like data into the writer/reader. IMHO, if the API says it takes a "string_view" but has some special undocumented preconditions then that is very brittle and bad API design. At the very least it would need to be documented. The (nice) alternative I see is to use a different type that does guarantee null termination, however I am not hyped about that because then I need to copy my strings in my parser for every backend. I think the better alternative I is to accept the data copies for now and petition at the libraries of "avro" and "flexbuf" to add "string_view" APIs. That said, I am all for removing copies. In fact, there is a lot of copies already anyway. Many backends seem to actually copy the string again into their internal data structure even though e.g. the JSON library would provide a non-copy API. Furthermore when trying to input some string "string_view" as value, it too is copied.  | 
    
          
 By writing a parser, I meant specializing the "Parser" class. :)  | 
    
4693ccd    to
    aa6a8e4      
    Compare
  
    | 
           I fixed the issue hopefully. This might take a couple rounds because unfortunately I can't compile/test most backends locally.  | 
    
| 
           A crap, YAML is also failing because the API I am using is unreleased: The last release 0.8 is from 2023 and does not contain that.  | 
    
| 
           @ActuallyaDeviloper , I agree, it is brittle design. For Avro I don't really care that your changes introduce overhead. This format is slow AF anyway. But flexbuf is one of the two fastest formats supported (the other one being msgpack). Once the code fully compiles, we should take a look at the benchmarks to see how much of an impact this makes. Then, we can make a decision how to proceed here.  | 
    
          
 Yeah, YAML is fine as well. This is the slowest format we have and no one who uses YAML really cares about speed.  | 
    
…t is really not. Also avoid recomputing the string length for APIs that allow that.
aa6a8e4    to
    26d581f      
    Compare
  
    
          
 Can you restart the testing?  | 
    
| 
           Related to #444 honestly my solution is to adopt something like C++29 zstring_view it is 0 cost string view wrapper  | 
    
Hello, when writing my own parser (reader/writer) support, I noticed that the library incorrectly assumes that "string_view"s are nullterminated. They aren't (sometimes). Also the string length is sometimes recomputed when it's not really necessary.
I was gonna report an issue but then I decided to just fix it myself. Only problem is that I cannot test the other backends (which I do not intend to use) locally right now. Unsurprisingly, the installation scripts fail deep inside some third party dependencies. I do not want to debug that.