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

validation: assert that pindexPrev is non-null when required #14834

Conversation

@kallewoof
Copy link
Member

@kallewoof kallewoof commented Nov 29, 2018

In ContextualCheckBlock, we are checking if pindexPrev == nullptr conditionally at the start, but then assume it is non-null later. This removes the latter assumption.

@luke-jr
Copy link
Member

@luke-jr luke-jr commented Nov 29, 2018

Is it even hypothetically possible for LOCKTIME_MEDIAN_TIME_PAST to be set on a genesis block?

Precedence is non-obvious in this case, so prefer adding parenthesis for clarity at least.

@kallewoof
Copy link
Member Author

@kallewoof kallewoof commented Nov 29, 2018

That's a good point. Is that the only case ContextualCheckBlock is called with pindexPrev == nullptr? (When block is the genesis block)

@luke-jr
Copy link
Member

@luke-jr luke-jr commented Nov 29, 2018

     const int nHeight = pindexPrev == nullptr ? 0 : pindexPrev->nHeight + 1;

@kallewoof
Copy link
Member Author

@kallewoof kallewoof commented Nov 29, 2018

Yeah, that pretty much proves it. It could change in the future, though, so I think the change is still warranted. It also plugs a static analyzer warning about null dereferencing, so there is that.

@kallewoof kallewoof force-pushed the 20181129-contextualcheckblock-pindexprev-nullness branch from 1c39f45 to 3e22420 Nov 29, 2018
@luke-jr
Copy link
Member

@luke-jr luke-jr commented Nov 29, 2018

If it changes in the future, your modification here is a security vulnerability, since the block must be checked with the correct min locktime.

@kallewoof kallewoof force-pushed the 20181129-contextualcheckblock-pindexprev-nullness branch from 3e22420 to 0162e54 Nov 29, 2018
@kallewoof kallewoof changed the title validation: do not break assumption that pindexPrev may be null validation: assert that pindexPrev is non-null when required Nov 29, 2018
@kallewoof
Copy link
Member Author

@kallewoof kallewoof commented Nov 29, 2018

@luke-jr I changed the approach and am now asserting that pindexPrev is non-null for the case where the lock time flags have LOCKTIME_MEDIAN_TIME_PAST. That seems more aligned with your reasoning, I think, and I agree makes more sense in this case.

@luke-jr
Copy link
Member

@luke-jr luke-jr commented Nov 29, 2018

I guess that looks okay, although I found the logic relatively hard to follow.

@kallewoof
Copy link
Member Author

@kallewoof kallewoof commented Nov 29, 2018

Actually, I can just move it into the if block above. Doing.

@kallewoof kallewoof force-pushed the 20181129-contextualcheckblock-pindexprev-nullness branch from 0162e54 to fbaaf78 Nov 29, 2018
@luke-jr
Copy link
Member

@luke-jr luke-jr commented Nov 29, 2018

utACK

@practicalswift
Copy link
Contributor

@practicalswift practicalswift commented Nov 29, 2018

utACK fbaaf78

Very nice to get rid of this warning and thereby increase the signal-to-noise in static analyzer output! Explicit assumptions are better than implicit assumptions. Thanks!

Copy link
Member

@promag promag left a comment

utACK fbaaf78.

@kallewoof
Copy link
Member Author

@kallewoof kallewoof commented Nov 29, 2018

@promag Maybe I'm not looking closely enough -- how does that improve the patch?

@promag
Copy link
Member

@promag promag commented Nov 29, 2018

@kallewoof yeah, it doesn't improve this patch, I've updated my comment shortly after, but now I've removed the suggestion.

@kallewoof
Copy link
Member Author

@kallewoof kallewoof commented Nov 29, 2018

Gotcha!

@Empact
Copy link
Member

@Empact Empact commented Dec 4, 2018

utACK fbaaf78

2 similar comments
@MarcoFalke
Copy link
Member

@MarcoFalke MarcoFalke commented Dec 4, 2018

utACK fbaaf78

@jonasschnelli
Copy link
Contributor

@jonasschnelli jonasschnelli commented Dec 9, 2018

utACK fbaaf78

@laanwj laanwj merged commit fbaaf78 into bitcoin:master Dec 13, 2018
2 checks passed
laanwj added a commit that referenced this issue Dec 13, 2018
…uired

fbaaf78 validation: assert that pindexPrev is non-null when required (Karl-Johan Alm)

Pull request description:

  In `ContextualCheckBlock`, we are checking if `pindexPrev == nullptr` conditionally at the start, but then assume it is non-`null` later. This removes the latter assumption.

Tree-SHA512: 95f1e9dc839b2cc0e099d155e6180634ece8c6760d00b53e7d27128762e64c92e82d98a5f4a5786b48a4851b17cdbb4b667d3b6a99adb651256e2032de67d05c
@kallewoof kallewoof deleted the 20181129-contextualcheckblock-pindexprev-nullness branch Dec 13, 2018
PastaPastaPasta added a commit to PastaPastaPasta/dash that referenced this issue Jun 27, 2021
…hen required

fbaaf78 validation: assert that pindexPrev is non-null when required (Karl-Johan Alm)

Pull request description:

  In `ContextualCheckBlock`, we are checking if `pindexPrev == nullptr` conditionally at the start, but then assume it is non-`null` later. This removes the latter assumption.

Tree-SHA512: 95f1e9dc839b2cc0e099d155e6180634ece8c6760d00b53e7d27128762e64c92e82d98a5f4a5786b48a4851b17cdbb4b667d3b6a99adb651256e2032de67d05c
PastaPastaPasta added a commit to PastaPastaPasta/dash that referenced this issue Jun 28, 2021
…hen required

fbaaf78 validation: assert that pindexPrev is non-null when required (Karl-Johan Alm)

Pull request description:

  In `ContextualCheckBlock`, we are checking if `pindexPrev == nullptr` conditionally at the start, but then assume it is non-`null` later. This removes the latter assumption.

Tree-SHA512: 95f1e9dc839b2cc0e099d155e6180634ece8c6760d00b53e7d27128762e64c92e82d98a5f4a5786b48a4851b17cdbb4b667d3b6a99adb651256e2032de67d05c
PastaPastaPasta added a commit to PastaPastaPasta/dash that referenced this issue Jun 29, 2021
…hen required

fbaaf78 validation: assert that pindexPrev is non-null when required (Karl-Johan Alm)

Pull request description:

  In `ContextualCheckBlock`, we are checking if `pindexPrev == nullptr` conditionally at the start, but then assume it is non-`null` later. This removes the latter assumption.

Tree-SHA512: 95f1e9dc839b2cc0e099d155e6180634ece8c6760d00b53e7d27128762e64c92e82d98a5f4a5786b48a4851b17cdbb4b667d3b6a99adb651256e2032de67d05c
PastaPastaPasta added a commit to PastaPastaPasta/dash that referenced this issue Jul 1, 2021
…hen required

fbaaf78 validation: assert that pindexPrev is non-null when required (Karl-Johan Alm)

Pull request description:

  In `ContextualCheckBlock`, we are checking if `pindexPrev == nullptr` conditionally at the start, but then assume it is non-`null` later. This removes the latter assumption.

Tree-SHA512: 95f1e9dc839b2cc0e099d155e6180634ece8c6760d00b53e7d27128762e64c92e82d98a5f4a5786b48a4851b17cdbb4b667d3b6a99adb651256e2032de67d05c
PastaPastaPasta added a commit to PastaPastaPasta/dash that referenced this issue Jul 1, 2021
…hen required

fbaaf78 validation: assert that pindexPrev is non-null when required (Karl-Johan Alm)

Pull request description:

  In `ContextualCheckBlock`, we are checking if `pindexPrev == nullptr` conditionally at the start, but then assume it is non-`null` later. This removes the latter assumption.

Tree-SHA512: 95f1e9dc839b2cc0e099d155e6180634ece8c6760d00b53e7d27128762e64c92e82d98a5f4a5786b48a4851b17cdbb4b667d3b6a99adb651256e2032de67d05c
PastaPastaPasta added a commit to PastaPastaPasta/dash that referenced this issue Jul 1, 2021
…hen required

fbaaf78 validation: assert that pindexPrev is non-null when required (Karl-Johan Alm)

Pull request description:

  In `ContextualCheckBlock`, we are checking if `pindexPrev == nullptr` conditionally at the start, but then assume it is non-`null` later. This removes the latter assumption.

Tree-SHA512: 95f1e9dc839b2cc0e099d155e6180634ece8c6760d00b53e7d27128762e64c92e82d98a5f4a5786b48a4851b17cdbb4b667d3b6a99adb651256e2032de67d05c
UdjinM6 added a commit to UdjinM6/dash that referenced this issue Jul 5, 2021
…hen required

fbaaf78 validation: assert that pindexPrev is non-null when required (Karl-Johan Alm)

Pull request description:

  In `ContextualCheckBlock`, we are checking if `pindexPrev == nullptr` conditionally at the start, but then assume it is non-`null` later. This removes the latter assumption.

Tree-SHA512: 95f1e9dc839b2cc0e099d155e6180634ece8c6760d00b53e7d27128762e64c92e82d98a5f4a5786b48a4851b17cdbb4b667d3b6a99adb651256e2032de67d05c
PastaPastaPasta added a commit to PastaPastaPasta/dash that referenced this issue Jul 8, 2021
…hen required

fbaaf78 validation: assert that pindexPrev is non-null when required (Karl-Johan Alm)

Pull request description:

  In `ContextualCheckBlock`, we are checking if `pindexPrev == nullptr` conditionally at the start, but then assume it is non-`null` later. This removes the latter assumption.

Tree-SHA512: 95f1e9dc839b2cc0e099d155e6180634ece8c6760d00b53e7d27128762e64c92e82d98a5f4a5786b48a4851b17cdbb4b667d3b6a99adb651256e2032de67d05c
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Sep 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

9 participants