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 memory leak enumerator #2174

Merged
merged 3 commits into from Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions Changelog.txt
@@ -1,3 +1,6 @@
11.8.2 -
Fix memory leak in NotEmptyValidator/EmptyValidator (#2174)

11.8.1 - 22 Nov 2023
Fix unintentional behavioural changes in introduced in the previous release as part of #2158

Expand Down
11 changes: 9 additions & 2 deletions src/FluentValidation/Validators/EmptyValidator.cs
Expand Up @@ -23,7 +23,6 @@ namespace FluentValidation.Validators;
using System;
using System.Collections;
using System.Collections.Generic;
using Resources;

public class EmptyValidator<T,TProperty> : PropertyValidator<T,TProperty> {

Expand All @@ -42,7 +41,7 @@ public class EmptyValidator<T,TProperty> : PropertyValidator<T,TProperty> {
return true;
}

if (value is IEnumerable e && !e.GetEnumerator().MoveNext()) {
if (value is IEnumerable e && IsEmpty(e)) {
return true;
}

Expand All @@ -52,4 +51,12 @@ public class EmptyValidator<T,TProperty> : PropertyValidator<T,TProperty> {
protected override string GetDefaultMessageTemplate(string errorCode) {
return Localized(errorCode, Name);
}

private static bool IsEmpty(IEnumerable enumerable) {
var enumerator = enumerable.GetEnumerator();

using (enumerator as IDisposable) {
return !enumerator.MoveNext();
}
}
}
10 changes: 9 additions & 1 deletion src/FluentValidation/Validators/NotEmptyValidator.cs
Expand Up @@ -41,7 +41,7 @@ public class NotEmptyValidator<T,TProperty> : PropertyValidator<T, TProperty>, I
return false;
}

if (value is IEnumerable e && !e.GetEnumerator().MoveNext()) {
if (value is IEnumerable e && IsEmpty(e)) {
return false;
}

Expand All @@ -51,6 +51,14 @@ public class NotEmptyValidator<T,TProperty> : PropertyValidator<T, TProperty>, I
protected override string GetDefaultMessageTemplate(string errorCode) {
return Localized(errorCode, Name);
}

private static bool IsEmpty(IEnumerable enumerable) {
var enumerator = enumerable.GetEnumerator();

using (enumerator as IDisposable) {
return !enumerator.MoveNext();
}
}
}

public interface INotEmptyValidator : IPropertyValidator {
Expand Down