From cd0a6c869064defafdab245b6aa23c1f88f0daf2 Mon Sep 17 00:00:00 2001 From: Storm Date: Tue, 10 Mar 2026 20:22:07 -0400 Subject: [PATCH 1/4] added check to make sure that votes are not empty --- main.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/main.go b/main.go index ecfe957..5dff5ae 100644 --- a/main.go +++ b/main.go @@ -404,6 +404,14 @@ func main() { return } } + + // Make sure vote is not empty + if len(vote.Options) == 0 { + c.JSON(http.StatusBadRequest, gin.H{"error": "You did not rank any options"}) + return + } + + // Submit Vote database.CastRankedVote(c, &vote, &voter) } else { c.JSON(http.StatusInternalServerError, gin.H{"error": "Unknown Poll Type"}) From 128fefdef2e928084184cc3ced631c783ff5c7db Mon Sep 17 00:00:00 2001 From: Storm Date: Fri, 13 Mar 2026 17:25:26 -0400 Subject: [PATCH 2/4] moved empty check logic and added comments --- main.go | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/main.go b/main.go index 5dff5ae..5e0f6ec 100644 --- a/main.go +++ b/main.go @@ -346,6 +346,7 @@ func main() { UserId: claims.UserInfo.Username, } + // Populate vote for _, option := range poll.Options { optionRankStr := c.PostForm(option) optionRank, err := strconv.Atoi(optionRankStr) @@ -360,7 +361,6 @@ func main() { vote.Options[option] = optionRank } - // process write-in if c.PostForm("writeinOption") != "" && c.PostForm("writein") != "" { for candidate := range vote.Options { @@ -380,10 +380,18 @@ func main() { } vote.Options[c.PostForm("writeinOption")] = rank } + // Perform checks, vote does not change beyond this maxNum := len(vote.Options) voted := make([]bool, maxNum) + // Make sure vote is not empty + if len(vote.Options) == 0 { + c.JSON(http.StatusBadRequest, gin.H{"error": "You did not rank any options"}) + return + } + + // Duplicate ranks and range check for _, rank := range vote.Options { if rank > 0 && rank <= maxNum { if voted[rank-1] { @@ -397,6 +405,7 @@ func main() { } } + // Too large of a rank rankedCandidates := len(vote.Options) for _, voteOpt := range vote.Options { if voteOpt > rankedCandidates { @@ -405,12 +414,6 @@ func main() { } } - // Make sure vote is not empty - if len(vote.Options) == 0 { - c.JSON(http.StatusBadRequest, gin.H{"error": "You did not rank any options"}) - return - } - // Submit Vote database.CastRankedVote(c, &vote, &voter) } else { From d62d9b5ea3412153ca2182d01690cfded7330465 Mon Sep 17 00:00:00 2001 From: Storm Date: Fri, 13 Mar 2026 17:37:40 -0400 Subject: [PATCH 3/4] removed duplicate len(vote.Options) --- main.go | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/main.go b/main.go index 5e0f6ec..5a0664b 100644 --- a/main.go +++ b/main.go @@ -382,33 +382,32 @@ func main() { } // Perform checks, vote does not change beyond this - maxNum := len(vote.Options) - voted := make([]bool, maxNum) + optionCount := len(vote.Options) + voted := make([]bool, optionCount) // Make sure vote is not empty - if len(vote.Options) == 0 { + if optionCount == 0 { c.JSON(http.StatusBadRequest, gin.H{"error": "You did not rank any options"}) return } // Duplicate ranks and range check for _, rank := range vote.Options { - if rank > 0 && rank <= maxNum { + if rank > 0 && rank <= optionCount { if voted[rank-1] { c.JSON(http.StatusBadRequest, gin.H{"error": "You ranked two or more candidates at the same level"}) return } voted[rank-1] = true } else { - c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("votes must be from 1 - %d", maxNum)}) + c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("votes must be from 1 - %d", optionCount)}) return } } // Too large of a rank - rankedCandidates := len(vote.Options) for _, voteOpt := range vote.Options { - if voteOpt > rankedCandidates { + if voteOpt > optionCount { c.JSON(http.StatusBadRequest, gin.H{"error": "Rank choice is more than the amount of candidates ranked"}) return } From abc69fe335ea8992b3af6bba8ef32d68dcec4ef6 Mon Sep 17 00:00:00 2001 From: Storm Date: Fri, 13 Mar 2026 17:38:42 -0400 Subject: [PATCH 4/4] removed duplicate range check and updated error message --- main.go | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/main.go b/main.go index 5a0664b..3dcf3e7 100644 --- a/main.go +++ b/main.go @@ -400,15 +400,7 @@ func main() { } voted[rank-1] = true } else { - c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("votes must be from 1 - %d", optionCount)}) - return - } - } - - // Too large of a rank - for _, voteOpt := range vote.Options { - if voteOpt > optionCount { - c.JSON(http.StatusBadRequest, gin.H{"error": "Rank choice is more than the amount of candidates ranked"}) + c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("Candidates chosen must be from 1 to %d", optionCount)}) return } }