Eliminate use of multiple-return from secp256k1.c. #218

pull gmaxwell wants to merge 2 commits into bitcoin-core:master from gmaxwell:a_goto_by_any_other_name changing 1 files +113 −102
  1. gmaxwell commented at 9:28 AM on February 17, 2015: contributor

    Goto, multiple returns, continue, and/or multiple breaks in a loop are often used to build complex or non-local control flow in software.

    (They're all basically the same thing, and anyone axiomatically opposing goto and not the rest is probably cargo-culting from the title of Dijkstra's essay without thinking hard about it.)

    Personally, I think the current use of these constructs in the code base is fine: no where are we using them to create control- flow that couldn't easily be described in plain English, which is hard to read or reason about, or which looks like a trap for future developers.

    Some, however, prefer a more rules based approach to software quality. In particular, MISRA forbids all of these constructs, and for good experience based reasons. Rule based criteria (as opposed to good judgement) also has the benefit of being machine checkable and surviving individual developers.

    (To be fair-- MISRA also has a process for accommodating code that breaks the rules for good reason).

    I think that in general we should also try to satisfy the rules- based measures of software quality, except where there is an objective reason not do: a measurable performance difference, logic that turns to spaghetti, etc.

    Changing out all the multiple returns in secp256k1.c appears to be basically neutral: Some parts become slightly less clear, some parts slightly more.

  2. gmaxwell commented at 9:29 AM on February 17, 2015: contributor

    For discussion: to complete this I should also go and get the short-cutting returns in the group logic, normalizes, think hard about the continue in wnaf, etc.

    I don't propose we hold the tests to the same standards.

  3. paveljanik commented at 9:43 AM on February 17, 2015: contributor

    @gmaxwell two first commits belong elsewhere.

  4. gmaxwell commented at 9:47 AM on February 17, 2015: contributor

    @paveljanik They are elsewhere. This is a "For discussion" commit. I'm not going to waste my time rewriting patch not on-top of the other changes just to make github happy.

  5. droark commented at 8:17 PM on February 17, 2015: none

    utACK for the multiple-return commit. I'd have done a couple of sections a bit differently. But, it really is six of one and half-a-dozen of the other. The patched code is readable and, as best I can tell, doesn't change the logic in a negative manner.

    Also, +1 for initializing the return variables. All variables ought to be initialized whenever possible.

  6. gmaxwell commented at 9:21 PM on February 17, 2015: contributor

    Also, +1 for initializing the return variables. All variables ought to be initialized whenever possible.

    I usually disagree there: initializing something unnecessarily undermines static analysis (compiler warnings) that the code is broken and its possible to use the variable in a way that wasn't intended. With the initialization you just silently get the default value. I considered it safe to do in this case because returning an error is 'safe'. But maybe this was a mistake, since for Bitcoin's purposes returning an error on a signature that was actually valid is a security vulnerability.

  7. sipa commented at 6:16 PM on February 25, 2015: contributor

    Needs rebase.

  8. gmaxwell commented at 11:07 PM on March 8, 2015: contributor

    rebased

  9. Make secp256k1_ec_pubkey_create reject oversized secrets. 354ffa33e6
  10. Eliminate multiple-returns from secp256k1.c.
    Goto, multiple returns, continue, and/or multiple breaks in a
     loop are often used to build complex or non-local control
     flow in software.
    
    (They're all basically the same thing, and anyone axiomatically
     opposing goto and not the rest is probably cargo-culting from
     the title of Dijkstra's essay without thinking hard about it.)
    
    Personally, I think the current use of these constructs in the
     code base is fine: no where are we using them to create control-
     flow that couldn't easily be described in plain English, which
     is hard to read or reason about, or which looks like a trap for
     future developers.
    
    Some, however, prefer a more rules based approach to software
     quality.  In particular, MISRA forbids all of these constructs,
     and for good experience based reasons.  Rules also have the
     benefit of being machine checkable and surviving individual
     developers.
    
    (To be fair-- MISRA also has a process for accommodating code that
     breaks the rules for good reason).
    
    I think that in general we should also try to satisfy the rules-
     based measures of software quality, except where there is an
     objective reason not do: a measurable performance difference,
     logic that turns to spaghetti, etc.
    
    Changing out all the multiple returns in secp256k1.c appears to
     be basically neutral:  Some parts become slightly less clear,
     some parts slightly more.
    0065a8fb9c
  11. sipa commented at 10:21 AM on March 16, 2015: contributor

    ACK

  12. sipa merged this on Mar 16, 2015
  13. sipa closed this on Mar 16, 2015

  14. sipa referenced this in commit d9b9f119e8 on Mar 16, 2015

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin-core/secp256k1. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-04-14 11:15 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me