Skip to content
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

[TRIVIAL] fix clang unused-but-set-variable warning #4677

Merged
merged 2 commits into from
Oct 31, 2023

Conversation

StefanVK
Copy link
Contributor

High Level Overview of Change

Fix clang warning about unused but set variable: removed variable.

Context of Change

(clang 15)
/workspaces/rippled/src/ripple/app/rdb/backend/impl/PostgresDatabase.cpp:178:14: warning: variable 'expNumResults' set but not used [-Wunused-but-set-variable]
uint32_t expNumResults = 1;

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Refactor (non-breaking change that only restructures code)
  • Tests (You added tests for code that already exists, or your new feature included in this PR)
  • Documentation Updates
  • Release

(clang 15)
/workspaces/rippled/src/ripple/app/rdb/backend/impl/PostgresDatabase.cpp:178:14: warning: variable 'expNumResults' set but not used [-Wunused-but-set-variable]
    uint32_t expNumResults = 1;
@StefanVK StefanVK changed the title [Trivial] fix clang unused-but-set-variable warning [TRIVIAL] fix clang unused-but-set-variable warning Aug 27, 2023
Copy link
Collaborator

@scottschurr scottschurr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 LGTM. Thanks for the contribution!

@intelliot intelliot requested a review from ckeshava October 2, 2023 23:44
@@ -189,8 +187,6 @@ loadLedgerInfos(
auto minAndMax =
std::get_if<std::pair<uint32_t, uint32_t>>(&whichLedger))
{
expNumResults = minAndMax->second - minAndMax->first;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it useful to assert the validity of the output of the Postgres database?
expNumResults could be set to an initial absurd value (say -1), later we can do:

if(expNumResults != -1)
    assert(expNumResults == res.ntuples());

Similar to the validation at

if (res.isNull() || res.ntuples() == 0)

Is this useful?

@intelliot
Copy link
Collaborator

Hi @StefanVK - could you address the comment above?

@intelliot intelliot modified the milestones: 2.0, 2024 release Oct 13, 2023
@StefanVK
Copy link
Contributor Author

@ckeshava and @intelliot
I'm not sure I'm the right person to answer this since I don't know the invariants of the database nor the intended preconditions of the function. I only suggested an obviously behavior preserving fix to a compiler warning here.

I myself would not feel comfortable adding that assert since it's not obvious to me that it's a failure to call this function with a range that exceeds the ledger range in the database. Returning whichever subset of LedgerInfos we have in the database seems like reasonable behavior to me. It's not clear to me that calling the function with a range that is not completely in our database is bug in the calling code and we can and do handle the case gracefully in loadLedgerInfos.

If you can argue that it is indeed always a bug to call loadLedgeInfos with a ledgerRange or seq that is not or not fully contained in our database, go ahead and add an assert. I'm not saying it's wrong to add the assert, just that I don't know enough about the function to feel comfortable adding it.

@ckeshava
Copy link
Collaborator

ckeshava commented Oct 23, 2023

Hello,
I have a couple of thoughts.

  1. loadLedgerHelper does not accurately reflect the input-possibilities for the whichLedger parameter. Although loadLedgerInfo supports a pair of minLedgerSeq and maxLedgerSeq as inputs, that is not reflected in the loadLedgerHelper function signature. Hence, validation at loadLedgerHelper is not exhaustive.

  2. PostgresDatabaseImp::getHashesByIndex make use of the above-mentioned pair of indices as input. But it does not perform sanity checks on the output. Not all the downstream callers of this function perform the sanity checks here and here

  3. Suppose the user asks for ledgers between sequence numbers 100 to 200 -- and rippled returns only 87 such ledgers, due to lack of relevant data. Is this situation possible? Does rippled have any fail-safe in this scenario? If not, I'd recommend adding this assert.

I have written the suggested changes at the tip of this branch

@scottschurr I'm not familiar with the databases used in rippled. What do you think?

@scottschurr
Copy link
Collaborator

@ckeshava, I get the feeling that your questions are expanding far beyond the boundaries of the initial change request. All of the suggested changes are inside of a

#ifdef RIPPLED_REPORTING

conditional macro block. Any questions regarding Postgres should be addressed to @mtrippled, who developed reporting mode.

However, fundamentally, this pull request is about a C++ variable that ...

  1. Is initialized, and
  2. Is possibly assigned to later, but
  3. Is never read from.

It is possible that the lack of reads of the variable points out a bug in the code. Only @mtrippled is qualified to answer that question. But, lacking that bug, removing the local variable expNumResults is a sensible call.

@mtrippled, could you look at the code in question and decide whether you want expNumResults removed, or if you think that value should be used later down in the code? Thanks.

@intelliot intelliot requested a review from mtrippled October 25, 2023 15:50
Copy link
Collaborator

@mtrippled mtrippled left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. 👍

@mtrippled
Copy link
Collaborator

@ckeshava What's the answer to your 3rd question?

@ckeshava
Copy link
Collaborator

@mtrippled I don't know the answer myself, I wanted to verify if that situation is possible. If we don't have all the relevant data in the local database, does rippled attempt to fetch the appropriate ledgers from its peers? I vaguely remember FLR and synchronization deal with this problem, but I don't know

@mtrippled
Copy link
Collaborator

@ckeshava I don't have the answer either. But as Scott alluded, before killing the process, make sure of a couple of things first:

  1. it always represents an error, and especially
  2. killing the process is the best way to handle the error

Generally, criteria 2 is only met if the alternative is incorrect data being persisted or returned to the user.

@ckeshava
Copy link
Collaborator

okay, I'll dig into it

@intelliot intelliot modified the milestones: 2024 release, 2.0 Oct 25, 2023
@ckeshava
Copy link
Collaborator

As it stands, I agree with the changes and it is okay to merge it.
I believe I can improve this portion of the codebase, I'll look into it after I'm done with the current PRs and open a separate refactor PR. 👍

@intelliot intelliot added Tech Debt Non-urgent improvements and removed Needs Discussion labels Oct 30, 2023
@intelliot intelliot merged commit 26b0322 into XRPLF:develop Oct 31, 2023
16 checks passed
sophiax851 pushed a commit to sophiax851/rippled that referenced this pull request Jun 12, 2024
With clang 15, an unused-but-set-variable warning was emitted:

PostgresDatabase.cpp:178:14: warning: variable 'expNumResults' set but
not used [-Wunused-but-set-variable]
    uint32_t expNumResults = 1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Tech Debt Non-urgent improvements
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

5 participants