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

fix: typed const conversion validation #3117

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open

Conversation

petar-dambovaliev
Copy link
Contributor

@petar-dambovaliev petar-dambovaliev commented Nov 13, 2024

During preprocessing, validates if typed constants are convertible.
Fixes issue

Typed constants are convertible only in a lossless way.
That means that we can convert floats to integers if the fractional part of the float is 0.

@github-actions github-actions bot added the 📦 🤖 gnovm Issues or PRs gnovm related label Nov 13, 2024
Copy link

codecov bot commented Nov 13, 2024

Codecov Report

Attention: Patch coverage is 15.62500% with 216 lines in your changes missing coverage. Please review.

Project coverage is 63.65%. Comparing base (bd1d76e) to head (6939e75).

Files with missing lines Patch % Lines
gnovm/pkg/gnolang/values_conversions.go 14.62% 195 Missing and 21 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3117      +/-   ##
==========================================
- Coverage   63.79%   63.65%   -0.14%     
==========================================
  Files         549      549              
  Lines       78819    79068     +249     
==========================================
+ Hits        50279    50333      +54     
- Misses      25150    25326     +176     
- Partials     3390     3409      +19     
Flag Coverage Δ
contribs/gnodev 60.54% <ø> (-0.63%) ⬇️
contribs/gnofaucet 15.77% <ø> (+0.94%) ⬆️
gno.land 73.70% <ø> (ø)
gnovm 67.53% <15.62%> (-0.40%) ⬇️
misc/genstd 79.72% <ø> (ø)
tm2 62.45% <ø> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

x := uint(tv.GetInt8())
tv.T = t
tv.SetUint(x)
case Uint8Kind:
validate(Int8Kind, Uint8Kind, func() bool { return tv.GetInt8() >= 0 })
Copy link
Member

Choose a reason for hiding this comment

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

When an int type value exceeding 255 comes in, it will exceed the range of uint8. I think overflow error should take precedence over convertable error - how do you think?

@@ -129,6 +129,136 @@ func TestBuiltinIdentifiersShadowing(t *testing.T) {
}
}

func TestConvertTo(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nitpicking: this function should be located in values_conversions_tests.go instead of gno_tests.go.

@@ -796,6 +796,6 @@ func (m *Machine) doOpFuncLit() {
func (m *Machine) doOpConvert() {
xv := m.PopValue()
t := m.PopValue().GetType()
ConvertTo(m.Alloc, m.Store, xv, t)
ConvertTo(m.Alloc, m.Store, xv, t, false)
Copy link
Contributor

@mvertes mvertes Nov 14, 2024

Choose a reason for hiding this comment

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

Here, we don't know if xv is a constant or not. Forcing false for isConst parameter here is incorrect for constant expressions. For example, the following sample should fail but it doesn't:

println(int(float64(1 + 0.5)))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in focus Core team is prioritizing this work 📦 🤖 gnovm Issues or PRs gnovm related
Projects
Status: In Review
Development

Successfully merging this pull request may close these issues.

bug(gnovm): prohibit conversion of non-convertible typed constants
4 participants