Skip to content

Commit 08ce373

Browse files
pwdelgithub-advanced-security[bot]astrosnat
authored
Potential fix for code scanning alert no. 12: Incorrect conversion between integer types (#484)
* Potential fix for code scanning alert no. 12: Incorrect conversion between integer types Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com> * Updating after further fix according to conventions. --------- Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com> Co-authored-by: Osnat Katz Moon <137817983+astrosnat@users.noreply.github.com>
1 parent 28fe94a commit 08ce373

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

README/README-CONVENTIONS.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,25 @@ db := util.GetDB()
160160

161161
#### Recommended Implementation
162162

163-
Use named constants to make the platform detection logic clear and self-documenting:
163+
**Option A: Direct Maximum Value Comparison (Recommended for Most Cases)**
164+
165+
This is the simplest and most readable approach:
164166

165167
```go
168+
// 32-bit platform compatibility check (Convention CONV-32BIT-001)
169+
// Ensure valueUint64 fits in a uint before casting
170+
if valueUint64 > uint64(^uint(0)) {
171+
return errors.New("value exceeds allowed range for uint platform type")
172+
}
173+
valueUint := uint(valueUint64)
174+
```
175+
176+
**Option B: Named Constants with Platform Detection (For Educational/Complex Cases)**
177+
178+
Use this when you need to understand or document the platform detection mechanism:
179+
180+
```go
181+
// 32-bit platform compatibility check (Convention CONV-32BIT-001)
166182
// Platform detection constants for 32-bit compatibility check
167183
const (
168184
bitsInByte = 8
@@ -184,6 +200,10 @@ if isPlatform32Bit && valueUint64 > math.MaxUint32 {
184200
valueUint := uint(valueUint64)
185201
```
186202

203+
**When to Use Each Approach:**
204+
- **Option A**: Use for most cases - simpler, more readable, equally effective
205+
- **Option B**: Use when the platform detection logic needs to be explicit for educational purposes or when working with security-critical code where the mechanism should be transparent
206+
187207
#### What NOT To Do (Avoid Magic Numbers)
188208

189209
```go

backend/handlers/math/positions/positionsmath.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package positionsmath
22

33
import (
4-
"socialpredict/errors"
4+
"errors"
55
"socialpredict/handlers/marketpublicresponse"
66
marketmath "socialpredict/handlers/math/market"
77
"socialpredict/handlers/math/outcomes/dbpm"
@@ -10,6 +10,8 @@ import (
1010
"socialpredict/models"
1111
"strconv"
1212

13+
spErrors "socialpredict/errors"
14+
1315
"gorm.io/gorm"
1416
)
1517

@@ -39,15 +41,20 @@ func CalculateMarketPositions_WPAM_DBPM(db *gorm.DB, marketIdStr string) ([]Mark
3941

4042
// marketIDUint for needed areas
4143
marketIDUint64, err := strconv.ParseUint(marketIdStr, 10, 64)
42-
if errors.ErrorLogger(err, "Can't convert string.") {
44+
if spErrors.ErrorLogger(err, "Can't convert string.") {
4345
return nil, err
4446
}
4547

48+
// 32-bit platform compatibility check (Convention CONV-32BIT-001)
49+
// Ensure marketIDUint64 fits in a uint before casting
50+
if marketIDUint64 > uint64(^uint(0)) {
51+
return nil, errors.New("marketIdStr value exceeds allowed range for uint platform type")
52+
}
4653
marketIDUint := uint(marketIDUint64)
4754

4855
// Assuming a function to fetch the market creation time
4956
publicResponseMarket, err := marketpublicresponse.GetPublicResponseMarketByID(db, marketIdStr)
50-
if errors.ErrorLogger(err, "Can't convert marketIdStr to publicResponseMarket.") {
57+
if spErrors.ErrorLogger(err, "Can't convert marketIdStr to publicResponseMarket.") {
5158
return nil, err
5259
}
5360

0 commit comments

Comments
 (0)