Open AntGuy04 opened 1 year ago
You should check if a pointer can be dereferenced before dereferencing it, since dereferencing null pointers is a system thing which cannot be caught.
....
std::string url;
if(image2QRRequest->url) // <<-----------
url = image2QRRequest->url->c_str();
int strength;
if(image2QRRequest->strength) // <<----------- Int8 is not int
strength = image2QRRequest->strength;
....
The whole webserver crashes, if one of the json body params is specified as null with the error message as segmentation fault. I have caught all errors using a try catch in the controller, but this is not getting caught. In the object mapper I have kept objectMapper->getDeserializer()->getConfig()->allowUnknownFields = false. Is there any way to get a 4xx/5xx error instead if one of the params is specifically given the value null instead of the whole web server crashing. Eg request body where failure occurs: {"url":null, "strength":1}
My DTO is as follows:
include OATPP_CODEGEN_BEGIN(DTO)
class URLEncodeRequest : public oatpp::DTO {
};
include OATPP_CODEGEN_END(DTO)
And the controller is as follows:
ADD_CORS(image2QR, "*", "GET, POST, OPTIONS", "DNT, User-Agent, X-Requested-With, If-Modified-Since, Cache-Control, Content-Type, Range", "1728000") ENDPOINT("POST", "/encodeURL", image2QR, BODY_DTO(Object, URLEncodeRequest)) {
try{
auto dto = URLEncodeRequest::createShared();
EncodeResponse encodeResponse;
std::string url = image2QRRequest->url->c_str();
int strength = image2QRRequest->strength;
encodeResponse.encodedURL = encodeResponse.encodeURL(url, strength);
return createDtoResponse(Status::CODE_200, encodeResponse.encodedURL);
} catch(std::exception &e){
auto statusDto = StatusDto::createShared();
statusDto->status="Internal Server Error";
statusDto->code=500;
statusDto->message="Unknown Internal Server Error";
std::cout<<e.what();
return createDtoResponse(Status::CODE_500, statusDto);
}catch(...) {
auto statusDto = StatusDto::createShared();
statusDto->status="Internal Server Error";
statusDto->code=500;
statusDto->message="Unknown Internal Server Error";
return createDtoResponse(Status::CODE_500, statusDto);
}
AppComponent.cpp OATPP_CREATE_COMPONENT(std::shared_ptr, serverConnectionProvider)([] {
return oatpp::network::tcp::server::ConnectionProvider::createShared({"0.0.0.0", 8000, oatpp::network::Address::IP_4});
}());
And this along with the controller is being called in the main function. / Register Components in scope of run() method / AppComponent components;