drogonframework / drogon

Drogon: A C++14/17/20 based HTTP web application framework running on Linux/macOS/Unix/Windows
MIT License
11.44k stars 1.1k forks source link

Seg Fault when accessing larger numbers in orm find all #1874

Closed aamenmuh closed 9 months ago

aamenmuh commented 9 months ago

Hello, I am trying to build a library system using drogon and angular. The design is that I have a large psql database of books that is hosted online and accessed by Drogon. The main controller maps every 10 books to a page number. (In total, there are 4000 books (i.e. 400 pages). Accessing the first 7 pages is fine and works as expected in both Angular via accessing drogon's local host in the webpage. For any larger index, it gives a seg fault and ends the get request and closes the connection. Here is the controller code:

void Main::getAll(const HttpRequestPtr& req,
        std::function<void (const HttpResponsePtr &)> &&callback, int page)
{
    auto dbClientPtr = drogon::app().getDbClient("remotealabooks");
    drogon::orm::Mapper<drogon_model::books_m1q9::Books> mp(dbClientPtr);
    Json::Value ret;
    Json::Value tempRet;
    auto books = mp.limit(10).offset(page * 10).findAll();
    for (auto row : books)
    {
        tempRet["id"] = *(row.getId());
        tempRet["title"] = *(row.getTitle());
        tempRet["PublicationYear"] = *(row.getPublicationyear());
        tempRet["Pages"] = *(row.getPages());
        tempRet["Category"] = *(row.getCategory());
        tempRet["Subcategory"] = *(row.getSubcategory());
        tempRet["DDC"] = *(row.getDdc());
        tempRet["Condition"] = *(row.getCondition());
        tempRet["Quantity"] = *(row.getQuantity());
        tempRet["CreatedBy"] = *(row.getCreatedby());
        tempRet["Availability"] = *(row.getAvailability());
        //tempRet["Location"] = *(row.getLocation());
        tempRet["Author"] = *(row.getAuthor());
        ret.append(tempRet);
    }
    auto resp = HttpResponse::newHttpJsonResponse(ret);
    callback(resp);
}

Expected Not give any console output and display the json when accessing directly from drogon's url

Actual Seg Fauly displayed on index > 7.

Attempted fixes

I could not fix it, but I foud that the issue is in the line containing findAll. When commented out, everything else works fine, tho surely it does not give the desired output.

an-tao commented 9 months ago

the getXXX methods in ORM classes may return a nullptr when the according column value is null, you should give it a check to avoid crashing when dereferencing the shared_ptr.

aamenmuh commented 9 months ago

the getXXX methods in ORM classes may return a nullptr when the according column value is null, you should give it a check to avoid crashing when dereferencing the shared_ptr.

Oh thanks! I checked the internal working and it did indeed return a null pointer in one of my data points. No longer an issue.