mirror of
https://github.com/odin-lang/Odin.git
synced 2026-01-06 21:17:40 +00:00
encoding/base32: Fix padding validation for malformed input
Fix a bug where padding characters in the middle of input were not detected when there was no trailing padding. The "verify no padding in middle" check was inside `if padding_count > 0`, so inputs like "MZ===YTBMZXW6YTB" would incorrectly pass validation. Test case added for this edge case.
This commit is contained in:
@@ -153,15 +153,15 @@ decode :: proc(
|
||||
padding_count += 1
|
||||
}
|
||||
|
||||
// Verify no padding in the middle
|
||||
for i := 0; i < data_len - padding_count; i += 1 {
|
||||
if data[i] == byte(PADDING) {
|
||||
return nil, .Malformed_Input
|
||||
}
|
||||
}
|
||||
|
||||
// Check for proper padding and length combinations
|
||||
if padding_count > 0 {
|
||||
// Verify no padding in the middle
|
||||
for i := 0; i < data_len - padding_count; i += 1 {
|
||||
if data[i] == byte(PADDING) {
|
||||
return nil, .Malformed_Input
|
||||
}
|
||||
}
|
||||
|
||||
content_len := data_len - padding_count
|
||||
mod8 := content_len % 8
|
||||
required_padding: int
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
#+test
|
||||
package test_encoding_base32
|
||||
|
||||
import "core:testing"
|
||||
@@ -83,7 +82,16 @@ test_base32_decode_invalid :: proc(t: ^testing.T) {
|
||||
|
||||
// Section 3.2 - Padding requirements
|
||||
{
|
||||
// Padding must only be at end
|
||||
// Padding in middle without trailing padding
|
||||
input := "MZ===YTBMZXW6YTB" // '===' in middle, no trailing padding
|
||||
output, err := base32.decode(input)
|
||||
if output != nil {
|
||||
defer delete(output)
|
||||
}
|
||||
testing.expect_value(t, err, Error.Malformed_Input)
|
||||
}
|
||||
{
|
||||
// Padding must only be at end (with trailing padding)
|
||||
input := "MZ=Q===="
|
||||
output, err := base32.decode(input)
|
||||
if output != nil {
|
||||
@@ -228,4 +236,4 @@ test_base32_custom_alphabet :: proc(t: ^testing.T) {
|
||||
}
|
||||
testing.expect_value(t, err, Error.Invalid_Character)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user