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

Handle case when Bytes appends to self #6308

Merged
merged 24 commits into from
Sep 3, 2024
Merged

Conversation

bitzoic
Copy link
Member

@bitzoic bitzoic commented Jul 29, 2024

Description

When calling self.append(self) for Bytes, we experienced undefined behavior as the logic would clear self. Now if this case is encountered, the function will revert.

Checklist

  • I have linked to any relevant issues.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have updated the documentation where relevant (API docs, the reference, and the Sway book).
  • I have added tests that prove my fix is effective or that my feature works.
  • I have added (or requested a maintainer to add) the necessary Breaking* or New Feature labels where relevant.
  • I have done my best to ensure that my PR adheres to the Fuel Labs Code Review Standards.
  • I have requested a review from the relevant team or maintainers.

@bitzoic bitzoic added bug Something isn't working lib: std Standard library labels Jul 29, 2024
@bitzoic bitzoic self-assigned this Jul 29, 2024
@bitzoic bitzoic requested review from a team as code owners July 29, 2024 11:43
@SwayStar123
Copy link
Collaborator

This is not the behaviour id expect personally. Id expect this to duplicate the array.
eg psuedocode

let a = [1,2,3]
let b = a.append(a)
assert( b == [1,2,3,1,2,3])

@IGI-111
Copy link
Contributor

IGI-111 commented Jul 30, 2024

I think there's ambiguity here since appending should both add the elements to the slice it's called on and consume the slice argument it's called with. The combination of these two does lead to the identity function, but it is a surprising result.

Rust gets around this problem by making it simply illegal to append onto oneself since that would be a double mutable borrow.

C++ is the more similar memory model to us and this:

#include <string>

int main() {

  std::string str = "abc";
  str.append(str);
  printf("%s\n", str.c_str());

  return 0;
}

Does print abcabc.

I'm with @SwayStar123 here that doubling the bytes is the more intuitive result. Though it bares mention in the docs. And we should be clear about what the exact semantics are here.

@bitzoic
Copy link
Member Author

bitzoic commented Jul 30, 2024

I think there's ambiguity here since appending should both add the elements to the slice it's called on and consume the slice argument it's called with. The combination of these two does lead to the identity function, but it is a surprising result.

Rust gets around this problem by making it simply illegal to append onto oneself since that would be a double mutable borrow.

C++ is the more similar memory model to us and this:

#include <string>

int main() {

  std::string str = "abc";
  str.append(str);
  printf("%s\n", str.c_str());

  return 0;
}

Does print abcabc.

I'm with @SwayStar123 here that doubling the bytes is the more intuitive result. Though it bares mention in the docs. And we should be clear about what the exact semantics are here.

I think I would prefer Rust's approach here and make it illegal.

@K1-R1
Copy link
Member

K1-R1 commented Jul 30, 2024

I agree with @bitzoic that we should follow Rust's approach for append. The addition of a concat method could be valuable as a follow up PR

K1-R1
K1-R1 previously approved these changes Jul 30, 2024
@K1-R1 K1-R1 requested a review from a team July 30, 2024 17:05
@IGI-111
Copy link
Contributor

IGI-111 commented Aug 2, 2024

Making it illegal is not reasonable without a borrow checker. We can't reliably check that you are trying to mutate the same memory twice at once without heavy annotations.

Sway is like C++ in that way.

We could just panic, but that seems a lot less coherent that picking a correct output for the reflexive call and sticking to it.

@bitzoic
Copy link
Member Author

bitzoic commented Aug 2, 2024

We can't reliably check that you are trying to mutate the same memory twice at once without heavy annotations.

This is what line 696 does.

if __addr_of::<Self>(self) == __addr_of::<Self>(other) {

We could just panic, but that seems a lot less coherent that picking a correct output for the reflexive call and sticking to it.

This is what line 697 does

revert(0)

sway-lib-std/src/bytes.sw Outdated Show resolved Hide resolved
@bitzoic bitzoic requested a review from IGI-111 August 9, 2024 10:37
@bitzoic bitzoic requested a review from K1-R1 August 9, 2024 10:37
sway-lib-std/src/bytes.sw Outdated Show resolved Hide resolved
@SwayStar123 SwayStar123 self-assigned this Aug 29, 2024
@bitzoic bitzoic requested review from IGI-111 and xunilrj September 2, 2024 07:06
@SwayStar123 SwayStar123 merged commit 8cb7796 into master Sep 3, 2024
38 checks passed
@SwayStar123 SwayStar123 deleted the bitzoic-bytes-append-self branch September 3, 2024 10:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working lib: std Standard library
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants