Commit Graph

348 Commits

Author SHA1 Message Date
Zoltán Kéri
d6f4412dc3 encoding/base32: Fix style issues for CI 2024-12-31 18:59:39 +01:00
Zoltán Kéri
3d25128520 encoding/base32: Convert files to UTF-8 with Unix line endings 2024-12-30 15:31:57 +01:00
Zoltán Kéri
5ce6990077 encoding/base32: Add proper cleanup for encoded strings in tests
Add defer delete for encoded strings across all test procedures to ensure
proper cleanup and prevent memory leaks. This completes the memory
management improvements started in 591dd876.
2024-12-30 15:26:42 +01:00
Zoltán Kéri
8292509769 encoding/base32: Add custom alphabet test case
Add test case to verify custom alphabet support. The test uses a
decimal-uppercase alphabet (0-9, A-V) to test both encoding and decoding
with custom tables, including validation. This ensures the encode and
decode functions work correctly with custom encoding tables and
validation functions as documented.
2024-12-30 15:18:38 +01:00
Zoltán Kéri
591dd8765a encoding/base32: Remove incorrect defer delete in encode()
Remove premature deallocation of the output buffer which was causing
use-after-free behavior. The returned string needs to take ownership
of this memory, but the defer delete was freeing it before the
string could be used. This fixes issues with encoding that were
introduced by overly aggressive memory cleanup in 93238db2.
2024-12-30 12:00:38 +01:00
Zoltán Kéri
0d4c0064d9 encoding/base32: Add encode->decode roundtrip test
Add test_base32_roundtrip() to verify the encode->decode roundtrip
preserves data integrity. This test helps ensure our base32 implementation
correctly handles the full encode->decode cycle without data loss or
corruption.
2024-12-30 03:03:50 +01:00
Zoltán Kéri
c9c59edc64 encoding/base32: Move tests to base32_test.odin
Move existing test procedures to a dedicated test file for better
code organization and maintainability.
2024-12-29 23:35:01 +01:00
Zoltán Kéri
490f527005 encoding/base32: Expand DEC_TABLE to full 256 bytes
The decoding table was only 224 bytes which caused type mismatches when
using custom alphabets, so expand with zeroes to cover full byte range
while maintaining the same decoding logic.
2024-12-26 19:20:46 +01:00
Zoltán Kéri
88c0e62095 encoding/base32: Use ENC_TBL parameter consistently in encode()
Fix encoding to properly use provided encoding table parameter instead of
hardcoded `ENC_TABLE`.

This makes encode properly support custom alphabets as documented.
2024-12-26 14:48:02 +01:00
Zoltán Kéri
e7fb02a84a encoding/base32: Add custom validation support
Add support for custom alphabet validation through an optional validation
function parameter. The default validation follows RFC 4648 base32
alphabet rules (A-Z, 2-7).

This properly supports the documented ability to use custom alphabets.
2024-12-25 16:15:41 +01:00
Zoltán Kéri
8211a911db encoding/base32: Replace padding map with switch statement
Replace package-level map with a simple switch statement for padding
validation. This eliminates allocations we can't properly free while
maintaining the same validation logic.
2024-12-24 20:46:38 +01:00
Zoltán Kéri
e75a49f095 encoding/base32: Set optimization mode for decode() 2024-12-24 16:07:01 +01:00
Zoltán Kéri
93238db202 encoding/base32: Use consistent allocator and add proper cleanup
Fix memory handling throughout base32 package:
- Make padding map package-level constant (to avoid repeated allocs)
- Use passed allocator in encode's make() call
- Add defer delete for allocated memory in encode
- Add proper cleanup in test cases
- Fix memory cleanup of output buffers

The changes ensure consistent allocator usage and cleanup in both
implementation and tests.
2024-12-24 16:00:00 +01:00
Zoltán Kéri
f1f2ed3194 encoding/base32: Fix decode implementation per RFC 4648
Rework base32.decode() to properly handle all cases per RFC 4648:

- Fix error detection order:
  - Check minimum length first (Invalid_Length)
  - Check character validity (Invalid_Character)
  - Check padding and structure (Malformed_Input)

- Fix padding validation:
  - Add required padding length checks (2=6, 4=4, 5=3, 7=1 chars)
  - Ensure padding only appears at end
  - Fix handling of unpadded inputs

- Fix buffer handling:
  - Proper output buffer size calculation
  - Add bounds checking for buffer access
  - Add proper buffer validation

For example:
- "M" correctly returns Invalid_Length (too short)
- "mzxq====" correctly returns Invalid_Character (lowercase)
- "MZXQ=" correctly returns Malformed_Input (wrong padding)
- Unpadded input lengths must be multiples of 8

These changes make the decode function fully compliant with RFC 4648
requirements while providing proper error handling.
2024-12-24 15:52:33 +01:00
Zoltán Kéri
7672ac945a encoding/base32: Add RFC 4648 test suite
Add test suite based on RFC 4648 test vectors and validation rules:
- Add section 10 test vectors for valid encoding/decoding
- Add test cases for invalid character handling (section 3.2)
- Add test cases for padding validation (section 4)
- Add test cases for length requirements (section 6)

The test vectors verify that:
- Empty string encodes/decodes correctly
- Standard cases like "foo" -> "MZXW6===" work
- Invalid characters are rejected
- Missing or malformed padding is detected
- Invalid lengths are caught
2024-12-24 15:28:34 +01:00
Zoltán Kéri
b9338777e3 encoding/base32: Fix buffer allocation and bounds checking
Fix buffer allocation size calculation and add proper bounds checking to
ensure output buffer has sufficient space. This fixes crashes that could
occur with inputs like "AA" and other edge cases where the output buffer
was too small.

Remove #no_bounds_check as proper bounds checking is necessary for safe
error handling. The small performance trade-off is worth the improved
robustness.
2024-12-24 02:20:32 +01:00
Zoltán Kéri
8c761627c8 encoding/base32: Replace assertions with error returns
Replace assertions with proper error handling in base32.decode() to allow
programs to handle invalid input gracefully rather than crashing.

The function now returns ([]byte, Error) instead of just []byte.
2024-12-24 02:17:57 +01:00
Laytan Laats
7edd332993 fix #4536 - stack buffer overflow for size_of 0 types in struct unmarshal 2024-12-06 22:12:52 +01:00
gingerBill
e4bdc4ff7c Merge pull request #4562 from dozn/patch-1
Move Struct Field Zipping Outside of Loop
2024-12-06 14:21:01 +00:00
gingerBill
d452d37b93 Fix #4537 2024-12-05 15:51:05 +00:00
dozn
37fb2754a1 Move Struct Field Zipping Outside of JSON Token Loop 2024-12-05 07:23:34 -08:00
gingerBill
d0f87913e2 Fix #4549 2024-12-02 10:49:49 +00:00
o:tone
c3478e0002 fix unmarshal unhandled error (#4515)
* fix unmarshal unhandled error
2024-11-25 13:15:33 +01:00
jakubtomsu
86c84f2621 Fix a bug in hxa decoder found by the new check 2024-11-17 21:35:49 +01:00
gingerBill
26f6741674 Check for more errors in json.unmarshal 2024-11-14 16:45:36 +00:00
gingerBill
c440122d93 Remove relative types from json marshaller 2024-11-14 16:18:41 +00:00
Alexis Caraballo
31d5bc48a7 fix cbor.to_json always returning array of tuples for objects
to_json uses a small proc to check if all keys of an object are strings.
It was always returning false for any input.
2024-10-29 01:50:38 -03:00
Laytan Laats
4608d26e6e encoding/cbor: fix skipping of ignored/missing fields in struct unmarshal 2024-10-26 22:39:46 +02:00
Laytan Laats
793ef6012b encoding/cbor: handle binary having more fields than the struct by discarding 2024-10-25 20:37:55 +02:00
Tom Solberg
90a0c834bb cbor: fix capacity and ptr calculation for dynarray unmarshal 2024-10-20 21:27:16 +02:00
Laytan Laats
e79f94ca5e encoding/json: fix leak when string is unmarshalled into non-string field
Fixes #4391
2024-10-19 17:08:24 +02:00
flysand7
45d26dc7f9 [encoding/ini]: Add missing line terminator at the end of the section 2024-09-24 18:57:42 +11:00
Laytan Laats
788e4b2b6a core/encoding/cbor: allow unmarshalling non-string map keys 2024-09-11 15:57:56 +02:00
gingerBill
a4fd0c133e Merge pull request #4191 from laytan/improve-package-doc-comments
core: improve package doc comments for the documentation generator
2024-09-04 21:37:48 +01:00
jakubtomsu
7487d507be unmarshal bitset ints like cbor does 2024-09-04 11:08:45 +02:00
Laytan Laats
288312a812 core: improve package doc comments for the documentation generator 2024-09-03 19:59:04 +02:00
Laytan Laats
195259e88b fix some doc comments 2024-09-02 20:10:11 +02:00
Laytan Laats
bfedcd7035 encoding/csv: fix examples 2024-08-30 23:31:31 +02:00
Ed Yu
4e2bce0c83 Fix error for uuid to require 36 bytes instead 32 (4 bytes for dashes) 2024-08-26 10:25:10 -07:00
Laytan Laats
912f99abc8 encoding/cbor: various fixes
- "null" is the proper way to represent the nil value in the diagnostic
  format
- hex encoding in diagnostic format was wrong
- struct keys weren't sorted the right deterministic way
2024-08-09 21:56:54 +02:00
gingerBill
5f7fef92fa Update example to use err != nil instead 2024-08-04 12:25:50 +01:00
Ronald
1a6885c2a3 Tidy up code 2024-07-21 00:08:20 +01:00
Ronald
c3a57853e2 Ensure that values in ini map are unquoted 2024-07-21 00:00:47 +01:00
Jeroen van Rijn
fc5ce30f34 Allow json to unmarshal empty struct. 2024-07-21 00:37:11 +02:00
gingerBill
ba3d7ba5d3 Add core:encoding/ini to examples/all 2024-07-19 12:03:34 +01:00
Ronald
c768d0719a Remove unnecessary change
This was accidentally added, it was a change I made whilst testing.
2024-07-17 21:57:35 +01:00
Ronald
f04db7145c Fix memory leak in encoding/ini
A simple change that fixes a memory leak caused by not deleting all the
values in the map
2024-07-17 21:19:14 +01:00
Laytan Laats
28fac62a02 fix some bugs with -disable-assert 2024-07-16 18:44:18 +02:00
gingerBill
9d84e00502 Clean up unquote code 2024-07-14 22:25:26 +01:00
gingerBill
7703b37a1b Merge branch 'master' of https://github.com/odin-lang/Odin 2024-07-14 22:23:52 +01:00