...
Code Block |
---|
|
#include <fstream>
#include <string>
void f(std::istream &in) {
char buffer[32];
try {
in.read(buffer, 32sizeof(buffer));
} catch (std::ios_base::failure &e) {
// Handle error
}
std::string str(buffer);
// ...
} |
Compliant Solution
This compliant solution continues to assume assumes that the input from the file is exactly at most 32 characters, and instead . Instead of inserting a null terminator, it constructs the std::string
object based on the sizenumber of characters read from the input stream. If the size of the input is uncertain, it is better to use std::basic_istream<T>::readsome()
or a formatted input function, depending on need.
Code Block |
---|
|
#include <fstream>
#include <string>
void f(std::istream &in) {
char buffer[32];
try {
in.read(buffer, 32sizeof(buffer));
} catch (std::ios_base::failure &e) {
// Handle error
}
std::string str(buffer, 32in.gcount());
// ...
} |
Risk Assessment
...