script: Remove undocumented and unused operator+ #18612

pull MarcoFalke wants to merge 1 commits into bitcoin:master from MarcoFalke:2004-scriptNoAdd changing 3 files +24 −61
  1. MarcoFalke commented at 10:27 pm on April 12, 2020: member

    This operator has no documented use case and is also unused outside of test code. The test code and all other (imaginary) code that might use this operator is written more clear and concise by the existing CScript push operators for opcodes and data.

    Removing the operator is also going to protect against accidentally reintroducing bugs like this https://github.com/bitcoin/bitcoin/commit/6ff5f718b6a67797b2b3bab8905d607ad216ee21#diff-8458adcedc17d046942185cb709ff5c3L1135 (last time it was used).

  2. DrahtBot added the label Consensus on Apr 12, 2020
  3. DrahtBot added the label Tests on Apr 12, 2020
  4. in src/script/script.h:490 in dddde1c8a6 outdated
    472@@ -487,15 +473,6 @@ class CScript : public CScriptBase
    473         return *this;
    474     }
    475 
    476-    CScript& operator<<(const CScript& b)
    


    sipa commented at 0:08 am on April 13, 2020:

    Deleting this is dangerous, as attempts to call it will still work, except they’ll use the function above instead (CScript::operator<<(const std::vector<unsigned char>&)).

    I think it could be made into a static check too:

    Better solution:

    0CScript& operator<<(const CScript& b) = delete;
    

    MarcoFalke commented at 1:13 am on April 13, 2020:

    They shouldn’t use the above. I tested and this is the error:

     0error: invalid operands to binary expression ('CScript' and 'CScript')
     1
     2./script/script.h:431:14: note: candidate function not viable: no known conversion from 'CScript' to 'int64_t' (aka 'long') for 1st argument
     3    CScript& operator<<(int64_t b) { return push_int64(b); }
     4             ^
     5./script/script.h:433:14: note: candidate function not viable: no known conversion from 'CScript' to 'opcodetype' for 1st argument
     6    CScript& operator<<(opcodetype opcode)
     7             ^
     8./script/script.h:441:14: note: candidate function not viable: no known conversion from 'CScript' to 'const CScriptNum' for 1st argument
     9    CScript& operator<<(const CScriptNum& b)
    10             ^
    11./script/script.h:447:14: note: candidate function not viable: no known conversion from 'CScript' to 'const std::vector<unsigned char>' for 1st argument
    12    CScript& operator<<(const std::vector<unsigned char>& b)
    13             ^
    141 error generated.
    

    I think you wanted to say that the non-existent operator should be explicitly deleted just as it has been done for the constructor in e1a55690e66ca962179bc8170695b92af8a3caa8?

    If yes, I have done that. The error is now

     0error: overload resolution selected deleted operator '<<'
     1
     2./script/script.h:430:14: note: candidate function has been explicitly deleted
     3    CScript& operator<<(const CScript& b) = delete;
     4             ^
     5./script/script.h:432:14: note: candidate function not viable: no known conversion from 'CScript' to 'int64_t' (aka 'long') for 1st argument
     6    CScript& operator<<(int64_t b) { return push_int64(b); }
     7             ^
     8./script/script.h:434:14: note: candidate function not viable: no known conversion from 'CScript' to 'opcodetype' for 1st argument
     9    CScript& operator<<(opcodetype opcode)
    10             ^
    11./script/script.h:442:14: note: candidate function not viable: no known conversion from 'CScript' to 'const CScriptNum' for 1st argument
    12    CScript& operator<<(const CScriptNum& b)
    13             ^
    14./script/script.h:448:14: note: candidate function not viable: no known conversion from 'CScript' to 'const std::vector<unsigned char>' for 1st argument
    15    CScript& operator<<(const std::vector<unsigned char>& b)
    16             ^
    

    sipa commented at 1:19 am on April 13, 2020:
    @MarcoFalke Oh, I forgot that CScript isn’t a vector anymore (but a prevector), so indeed the function above isn’t viable. If we’d have an operator that pushes a prevector however (which would be implicitly added by #13062 for example), then having an explicitly deleted version for CScript still prevents it from being called.

    MarcoFalke commented at 2:01 am on April 13, 2020:

    Checked that with Span, the output looks like this:

     0overload resolution selected deleted operator '<<'
     1            script << s;
     2            ~~~~~~ ^  ~
     3./script/script.h:435:14: note: candidate function has been explicitly deleted
     4    CScript& operator<<(const CScript& b) = delete;
     5             ^
     6./script/script.h:452:14: note: candidate function
     7    CScript& operator<<(const Span& b)
     8             ^
     9./script/script.h:437:14: note: candidate function not viable: no known conversion from 'const CScript' to 'int64_t' (aka 'long') for 1st argument
    10    CScript& operator<<(int64_t b) { return push_int64(b); }
    11             ^
    12./script/script.h:439:14: note: candidate function not viable: no known conversion from 'const CScript' to 'opcodetype' for 1st argument
    13    CScript& operator<<(opcodetype opcode)
    14             ^
    15./script/script.h:447:14: note: candidate function not viable: no known conversion from 'const CScript' to 'const CScriptNum' for 1st argument
    16    CScript& operator<<(const CScriptNum& b)
    17             ^
    18./script/script.h:454:14: note: candidate function not viable: no known conversion from 'const CScript' to 'const std::vector<unsigned char>' for 1st argument
    19    CScript& operator<<(const std::vector<unsigned char>& b)
    20             ^
    

    sipa commented at 5:35 am on April 13, 2020:
    Not sure if you’re arguing in favor or against what I’m saying, but to be clear: marking it explicitly deleted is the right approach IMO (not just because it prevents reintroducing the problem, but also to make it easy to see this PR doesn’t change behavior).

    MarcoFalke commented at 10:15 am on April 13, 2020:
    I posted this for myself to remind me that your comment (https://github.com/bitcoin/bitcoin/pull/18612#issuecomment-612710105) is right
  5. MarcoFalke force-pushed on Apr 13, 2020
  6. in src/test/fuzz/script_ops.cpp:24 in 4444902f5c outdated
    17@@ -18,10 +18,10 @@ void test_one_input(const std::vector<uint8_t>& buffer)
    18     while (fuzzed_data_provider.remaining_bytes() > 0) {
    19         switch (fuzzed_data_provider.ConsumeIntegralInRange(0, 7)) {
    20         case 0:
    21-            script += ConsumeScript(fuzzed_data_provider);
    22+            (void)ConsumeScript(fuzzed_data_provider);
    23             break;
    24         case 1:
    25-            script = script + ConsumeScript(fuzzed_data_provider);
    26+            (void)ConsumeScript(fuzzed_data_provider);
    


    sipa commented at 1:31 am on April 13, 2020:
    I think if you’d merge these two into one call for both case 0 & case 1, the fuzzer might work slightly more efficiently by not making 0 and 1 take a different code path.

    ryanofsky commented at 5:31 pm on April 13, 2020:

    I think if you’d merge these two into one call for both case 0 & case 1, the fuzzer might work slightly more efficiently by not making 0 and 1 take a different code path.

    Tend to agree. If not going to collapse now could at least add a comment to avoid confusion here


    practicalswift commented at 6:44 pm on April 13, 2020:
    Isn’t the point here not to invalidate the existing seed corpus?

    sipa commented at 6:46 pm on April 13, 2020:

    I mean keep case 0 and case 1, but merge them into a single branch.

    0case 0:
    1case 1:
    2    (void)ConsumeScript(fuzzed_data_provider);
    

    MarcoFalke commented at 7:24 pm on April 13, 2020:
    I am planning to replace them by operator=(const& and operator=(&& respectively

    MarcoFalke commented at 11:23 pm on April 16, 2020:
    Replaced by operator=(const& and operator=(&& respectively
  7. sipa commented at 1:35 am on April 13, 2020: member

    utACK 4444902f5c4df8784cb2b781d82a55a07336e827

    It seems like a no-brainer to remove the +/+= if this is all they’re used for. As far as the operator<< is concerned, I believe that replacing any member function with a deleted version never changes semantics, if the result still compiles.

  8. DrahtBot commented at 4:42 pm on April 13, 2020: member

    The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

    Conflicts

    No conflicts as of last run.

  9. MarcoFalke removed the label Tests on Apr 13, 2020
  10. practicalswift commented at 5:13 pm on April 13, 2020: contributor

    Concept ACK

    Non-existing is better than unused/undocumented :)

  11. in src/test/script_tests.cpp:309 in 4444902f5c outdated
    305@@ -306,10 +306,10 @@ class TestBuilder
    306         return *this;
    307     }
    308 
    309-    TestBuilder& Add(const CScript& _script)
    310+    TestBuilder& Add(const opcodetype& _op)
    


    ryanofsky commented at 5:35 pm on April 13, 2020:
    Maybe this method should be called Opcode() instead of Add() to be consistent with the Num() method below and since it’s no longer doing + operation

    MarcoFalke commented at 7:25 pm on April 13, 2020:
    Will do when I rebase

    MarcoFalke commented at 11:23 pm on April 16, 2020:
    Renamed to Opcode
  12. ryanofsky approved
  13. ryanofsky commented at 5:36 pm on April 13, 2020: member
    Code review ACK 4444902f5c4df8784cb2b781d82a55a07336e827
  14. DrahtBot added the label Needs rebase on Apr 15, 2020
  15. script: Remove undocumented and unused operator+ ccccd51908
  16. MarcoFalke force-pushed on Apr 15, 2020
  17. DrahtBot removed the label Needs rebase on Apr 15, 2020
  18. MarcoFalke commented at 11:23 pm on April 16, 2020: member
    Rebased
  19. MarcoFalke closed this on Apr 16, 2020

  20. MarcoFalke reopened this on Apr 16, 2020

  21. laanwj commented at 12:04 pm on April 22, 2020: member
    ACK ccccd5190898ece3ac17aa3178f320d091f221df
  22. laanwj merged this on Apr 22, 2020
  23. laanwj closed this on Apr 22, 2020

  24. MarcoFalke deleted the branch on Apr 22, 2020
  25. MarcoFalke referenced this in commit 47b94a337e on Apr 22, 2020
  26. sidhujag referenced this in commit 3586208dd6 on Apr 23, 2020
  27. domob1812 referenced this in commit de280a19e4 on Apr 27, 2020
  28. Fabcien referenced this in commit 67d3d6330c on Jan 20, 2021
  29. sanket1729 referenced this in commit 1b0bdbb039 on Jul 21, 2021
  30. vijaydasmp referenced this in commit 1f8220b1c7 on Aug 22, 2021
  31. vijaydasmp referenced this in commit bd330ba98d on Aug 22, 2021
  32. vijaydasmp referenced this in commit f27bc7125f on Aug 22, 2021
  33. vijaydasmp referenced this in commit e51c067f43 on Aug 22, 2021
  34. darosior referenced this in commit f3d752650d on Aug 24, 2021
  35. vijaydasmp referenced this in commit 66e7abbc01 on Aug 29, 2021
  36. vijaydasmp referenced this in commit aa0e130d38 on Aug 29, 2021
  37. vijaydasmp referenced this in commit 12fef2db71 on Aug 29, 2021
  38. vijaydasmp referenced this in commit 079e4a38ae on Aug 29, 2021
  39. vijaydasmp referenced this in commit 7f95090820 on Aug 29, 2021
  40. vijaydasmp referenced this in commit 9b25a61b87 on Sep 1, 2021
  41. darosior referenced this in commit b252ef42f2 on Sep 29, 2021
  42. darosior referenced this in commit b6042f9d21 on Sep 29, 2021
  43. darosior referenced this in commit aa293218e6 on Dec 8, 2021
  44. darosior referenced this in commit 675c8ccd66 on Dec 18, 2021
  45. darosior referenced this in commit 9c7480653f on Jan 21, 2022
  46. darosior referenced this in commit e4711c23cb on Jan 25, 2022
  47. DrahtBot locked this on Feb 15, 2022

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2024-12-21 15:12 UTC

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