General TC > Programming & Scripting - Unofficial

How to define a variable compatible with npos in C++

<< < (2/3) > >>

Rich:
Hi Paul_123

--- Quote from: Paul_123 on May 10, 2025, 07:57:46 PM --- ... So what is the problem you are trying to solve?
--- End quote ---
I just wanted to know the return type  for .find().
I think this looks right:

--- Code: ---std::string::size_type sloc = cmdline.find(target_boot_option);
--- End code ---

gadget42:
mostly for future thread visitors:

https://stackoverflow.com/questions/918567/size-t-vs-containersize-type

https://en.cppreference.com/w/cpp/types/size_t

however, it should be duly noted that anything/everything _on_and_or_traveling_through_the_tentacles_of_the_www_cyber_realm can/may/might/will be altered and/or possibly hallucinated via AI/LLM/LVM/ML/etc (so basically everything on the internet should be considered suspect at the very least)

more (who watches the watchers that watch the "good" ai that watches out for the "bad" ai?)

even more (so we need even more climate-destroying energy-hungry data-centers for all those "good" ai to find the "bad" ai... and then do what? to what? where/when/how? and why are the people pointing this out not being taken more seriously?)

rhetorical? off-topic? maybe... maybe_not... quantum_ai... singularity... https://hmpg.net/ https://endoftheinternet.com/ please_turn_the_lights_off_on_your_way_out_ok_thanks_bye_

Paul_123:

--- Quote from: Rich on May 11, 2025, 12:48:55 AM ---I just wanted to know the return type  for .find().
I think this looks right:

--- Code: ---std::string::size_type sloc = cmdline.find(target_boot_option);
--- End code ---

--- End quote ---

That’s is what it’s defined as, but there is nothing really wrong casting to an int either

Rich:
Hi Paul_123
Thanks, I think I'll make all affected variables match the return type.

By the way, I think i may have found a potential bug in apps and appsaudit:

--- Code: ---string cmdline, target_boot_option;
ifstream proc_cmdline("/proc/cmdline");
getline(proc_cmdline, cmdline);
proc_cmdline.close();
target_boot_option = "lst=";
int sloc = cmdline.find(target_boot_option);
if ( sloc == string::npos ) {
   onbootName = "onboot.lst";
} else {
   int eloc = cmdline.find(" ",sloc);
   int work = eloc - (sloc + target_boot_option.length());
   onbootName = cmdline.substr(sloc+target_boot_option.length(),work);
}
--- End code ---
If I understand this correctly:

If "lst=" was found, we search for the next blank space separating boot options:

--- Code: ---int eloc = cmdline.find(" ",sloc);
--- End code ---
If someone placed the "lst=" option last, there is no blank space, and find returns  npos.

Here we calculate the length of the file name after "lst=":

--- Code: ---int work = eloc - (sloc + target_boot_option.length());
--- End code ---
If the previous command returned  npos , then  eloc  would be huge and so would  work.

This copies that file name to onbootName:

--- Code: ---onbootName = cmdline.substr(sloc+target_boot_option.length(),work);
--- End code ---
If  work  contains a huge value, we now fall off the end of the array and seg fault.

Did I get that right, or did I miss something?
Does C++ do something behind the scenes I'm not aware of?

Paul_123:
c++ string functions handles the end of the string more gracefully than c.

In your example work is in fact large but would produce the same result as using npos.  The substr function accepts npos as the end position, and just returns from the start position to the end of the string.


Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version