Skip to content

Commit 904c216

Browse files
authored
Merge pull request #9144 from NuGet/dev
[Bug]Fix potential overflow of statistics values. (#9128)
2 parents e92c2a9 + 174146f commit 904c216

25 files changed

Lines changed: 315 additions & 42 deletions

src/NuGet.Services.Entities/Package.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public Package()
5858
/// </remarks>
5959
public string ReleaseNotes { get; set; }
6060

61-
public int DownloadCount { get; set; }
61+
public long DownloadCount { get; set; }
6262

6363
/// <remarks>
6464
/// Is not a property that we support. Maintained for legacy reasons.

src/NuGet.Services.Entities/PackageRegistration.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public PackageRegistration()
2323
[Required]
2424
public string Id { get; set; }
2525

26-
public int DownloadCount { get; set; }
26+
public long DownloadCount { get; set; }
2727

2828
public bool IsVerified { get; set; }
2929

src/NuGetGallery.Core/Auditing/AuditedEntities/AuditedPackage.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class AuditedPackage
1414
public DateTime Created { get; private set; }
1515
public string Description { get; private set; }
1616
public string ReleaseNotes { get; private set; }
17-
public int DownloadCount { get; private set; }
17+
public long DownloadCount { get; private set; }
1818
public string ExternalPackageUrl { get; private set; }
1919
public string HashAlgorithm { get; private set; }
2020
public string Hash { get; private set; }

src/NuGetGallery.Core/Auditing/AuditedEntities/AuditedPackageRegistration.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace NuGetGallery.Auditing.AuditedEntities
88
public class AuditedPackageRegistration
99
{
1010
public string Id { get; private set; }
11-
public int DownloadCount { get; private set; }
11+
public long DownloadCount { get; private set; }
1212
public int Key { get; private set; }
1313
public bool IsVerified { get; private set; }
1414

src/NuGetGallery.Services/Models/PackageDependent.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace NuGetGallery
88
public class PackageDependent
99
{
1010
public string Id { get; set; }
11-
public int DownloadCount { get; set; }
11+
public long DownloadCount { get; set; }
1212
public string Description { get; set; }
1313
public bool IsVerified { get; set; }
1414
}

src/NuGetGallery/Areas/Admin/ViewModels/PackageSearchResult.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class PackageSearchResult
99
{
1010
public string PackageId { get; set; }
1111
public string PackageVersionNormalized { get; set; }
12-
public int DownloadCount { get; set; }
12+
public long DownloadCount { get; set; }
1313
public string Created { get; set; }
1414
public bool Listed { get; set; }
1515
public string PackageStatus { get; set; }

src/NuGetGallery/Extensions/NumberExtensions.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,16 @@ public static string ToUserFriendlyBytesLabel(this int bytes)
8383
/// <param name="number">The number to format</param>
8484
/// <returns>String representation of the formatted number</returns>
8585
public static string ToKiloFormat(this int number)
86+
{
87+
return ToKiloFormat((long)number);
88+
}
89+
90+
public static string ToKiloFormat(this long number)
8691
{
8792
// To avoid overflow (with Math.Abs()). 1 difference won't make a difference in the simplified format :)
88-
if (number == int.MinValue)
93+
if (number == long.MinValue)
8994
{
90-
number = -1 * int.MaxValue;
95+
number = -1 * long.MaxValue;
9196
}
9297

9398
if (Math.Abs(number) < 1000)
@@ -97,9 +102,10 @@ public static string ToKiloFormat(this int number)
97102

98103
var powers = new[]
99104
{
100-
new { Value = 1_000_000_000f, Unit = 'B'},
101-
new { Value = 1_000_000f , Unit = 'M'},
102-
new { Value = 1_000f , Unit = 'K'}
105+
new { Value = 1_000_000_000_000f, Unit = 'T'},
106+
new { Value = 1_000_000_000f , Unit = 'B'},
107+
new { Value = 1_000_000f , Unit = 'M'},
108+
new { Value = 1_000f , Unit = 'K'}
103109
};
104110

105111
return powers

src/NuGetGallery/Infrastructure/Lucene/ExternalSearchService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ internal static Package ReadPackage(JObject doc, string semVerLevel)
193193
Owners = reg.Value<JArray>("Owners")
194194
.Select(v => new User { Username = v.Value<string>() })
195195
.ToArray(),
196-
DownloadCount = reg.Value<int>("DownloadCount"),
196+
DownloadCount = reg.Value<long>("DownloadCount"),
197197
IsVerified = reg.Value<bool>("Verified"),
198198
Key = reg.Value<int>("Key")
199199
};
@@ -209,7 +209,7 @@ internal static Package ReadPackage(JObject doc, string semVerLevel)
209209
Created = doc.Value<DateTime>("Created"),
210210
Description = doc.Value<string>("Description"),
211211
Dependencies = dependencies,
212-
DownloadCount = doc.Value<int>("DownloadCount"),
212+
DownloadCount = doc.Value<long>("DownloadCount"),
213213
FlattenedAuthors = doc.Value<string>("Authors"),
214214
FlattenedDependencies = doc.Value<string>("FlattenedDependencies"),
215215
Hash = doc.Value<string>("Hash"),

src/NuGetGallery/Infrastructure/Lucene/LuceneSearchService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ private SearchResults SearchCore(SearchFilter searchFilter)
102102

103103
private static Package PackageFromDoc(Document doc)
104104
{
105-
int downloadCount = Int32.Parse(doc.Get("DownloadCount"), CultureInfo.InvariantCulture);
106-
int versionDownloadCount = Int32.Parse(doc.Get("VersionDownloadCount"), CultureInfo.InvariantCulture);
105+
long downloadCount = long.Parse(doc.Get("DownloadCount"), CultureInfo.InvariantCulture);
106+
long versionDownloadCount = long.Parse(doc.Get("VersionDownloadCount"), CultureInfo.InvariantCulture);
107107
int key = Int32.Parse(doc.Get("Key"), CultureInfo.InvariantCulture);
108108
int packageRegistrationKey = Int32.Parse(doc.Get("PackageRegistrationKey"), CultureInfo.InvariantCulture);
109109
int packageSize = Int32.Parse(doc.Get("PackageFileSize"), CultureInfo.InvariantCulture);

src/NuGetGallery/Migrations/202205300006043_Int64PackageDownloadCount.Designer.cs

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)