Skip to content

Commit e674fb2

Browse files
OskarEichlerSongstats Dependency Audit
andauthored
Validate AtomicFixnum update results before mutation (#1114)
* Validate AtomicFixnum update results * Test invalid AtomicFixnum update results --------- Co-authored-by: Songstats Dependency Audit <audit@local.invalid>
1 parent 609b172 commit e674fb2

3 files changed

Lines changed: 13 additions & 1 deletion

File tree

ext/concurrent-ruby-ext/atomic_fixnum.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ VALUE method_atomic_fixnum_update(VALUE self) {
7373
for (;;) {
7474
old_value = method_atomic_fixnum_value(self);
7575
new_value = rb_yield(old_value);
76+
Check_Type(new_value, T_FIXNUM);
7677
if (ir_compare_and_set(self, old_value, new_value) == Qtrue) {
7778
return new_value;
7879
}

lib/concurrent-ruby/concurrent/atomic/mutex_atomic_fixnum.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def compare_and_set(expect, update)
5555
# @!macro atomic_fixnum_method_update
5656
def update
5757
synchronize do
58-
@value = yield @value
58+
ns_set(yield @value)
5959
end
6060
end
6161

spec/concurrent/atomic/atomic_fixnum_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,17 @@
152152
atomic = described_class.new(1000)
153153
expect(atomic.update { |v| v + 1 }).to eq 1001
154154
end
155+
156+
it 'rejects a non-integer result without changing the value' do
157+
atomic = described_class.new(1000)
158+
159+
expect {
160+
atomic.update { 'not an integer' }
161+
}.to(raise_error { |error|
162+
expect(error.class).to be(ArgumentError).or(be(TypeError))
163+
})
164+
expect(atomic.value).to eq 1000
165+
end
155166
end
156167
end
157168

0 commit comments

Comments
 (0)