Title: Inconsistent result of failed Base64.decode (throws vs returns null) · Issue #65 · patrickfav/bytes-java · GitHub
Open Graph Title: Inconsistent result of failed Base64.decode (throws vs returns null) · Issue #65 · patrickfav/bytes-java
X Title: Inconsistent result of failed Base64.decode (throws vs returns null) · Issue #65 · patrickfav/bytes-java
Description: When Base64.decode(CharSequence in) decodes a string, in some cases for "bad" strings in throws IllegalArgumentException: bytes-java/src/main/java/at/favre/lib/bytes/Base64.java Line 92 in e622b3b throw new IllegalArgumentException("inva...
Open Graph Description: When Base64.decode(CharSequence in) decodes a string, in some cases for "bad" strings in throws IllegalArgumentException: bytes-java/src/main/java/at/favre/lib/bytes/Base64.java Line 92 in e622b3b ...
X Description: When Base64.decode(CharSequence in) decodes a string, in some cases for "bad" strings in throws IllegalArgumentException: bytes-java/src/main/java/at/favre/lib/bytes/Base64.java Line 92 i...
Opengraph URL: https://github.com/patrickfav/bytes-java/issues/65
X: @github
Domain: patch-diff.githubusercontent.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Inconsistent result of failed Base64.decode (throws vs returns null)","articleBody":"When `Base64.decode(CharSequence in)` decodes a string, in some cases for \"bad\" strings in throws `IllegalArgumentException`:\r\n\r\nhttps://github.com/patrickfav/bytes-java/blob/e622b3b4dfe0bca2b65d2dcd6dae78441ee2961a/src/main/java/at/favre/lib/bytes/Base64.java#L92\r\n\r\n, but in other cases it returns `null`.\r\n\r\nhttps://github.com/patrickfav/bytes-java/blob/e622b3b4dfe0bca2b65d2dcd6dae78441ee2961a/src/main/java/at/favre/lib/bytes/Base64.java#L108-L110\r\n\r\nThis makes the method harder to use as the caller needs to handle both cases for every call with potentially invalid input: catching `IllegalArgumentException` and comparing the result with `null`.\r\nI think the best way to handle both types of invalid input would be to throw `IllegalArgumentException` in both cases and never return `null`.\r\n\r\nThis behavior is even tested:\r\n\r\nhttps://github.com/patrickfav/bytes-java/blob/e622b3b4dfe0bca2b65d2dcd6dae78441ee2961a/src/test/java/at/favre/lib/bytes/Base64Test.java#L47\r\n\r\nIt makes me think that this is the desired behavior, but why?\r\n\r\nBtw, this behavior actually leads to the bug in the `id-mask` library. That is how I actually found it.\r\nWhen I do `idMask.unmask(badlyEncodedString)`, then it eventually calls `Base64.decode`, that returns `null`, and then it tries to wrap it into `Bytes` object\r\n\r\nhttps://github.com/patrickfav/bytes-java/blob/e622b3b4dfe0bca2b65d2dcd6dae78441ee2961a/src/main/java/at/favre/lib/bytes/Bytes.java#L155-L157\r\n\r\n, that leads to `java.lang.NullPointerException: passed array must not be null`. That is unexpected behavior I believe. `idMask.unmask(badlyEncodedString)` should never throw NPE.\r\n\r\nOf course, this problem could be solved on the `id-mask` side (by catching NPE and throwing `IllegalArgumentException`), but I think the root problem is the inconsistent behavior of `Base64.decode`. I can't imagine a situation when it is the desired behavior to sometimes throw `IllegalArgumentException` and sometimes return `null` for basically the same case of \"invalid input\".\r\n\r\nWhy it is important to throw `IllegalArgumentException` and not `NullPointerException`? Because the 1st error means the error is on the caller side (probably someone called the API with a wrong encoded ID), while the 2nd one means that something wrong happened inside the library. NPE would probably be acceptable in the case when `input` CharSequence is `null`. But if it is a non-null string, then NPE should never be thrown except for a bug in the implementation.","author":{"url":"https://github.com/xak2000","@type":"Person","name":"xak2000"},"datePublished":"2024-03-20T17:53:12.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/65/bytes-java/issues/65"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:5abebecb-5d13-b892-a406-9549f922dbf8 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D10A:2C1011:6D78DAD:9089FAD:696DD884 |
| html-safe-nonce | ee03ab8ef056703ff98dbd5d3d4dfc3656dfb4d1af882748b8eb2f23095d88dc |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEMTBBOjJDMTAxMTo2RDc4REFEOjkwODlGQUQ6Njk2REQ4ODQiLCJ2aXNpdG9yX2lkIjoiNjY4MDkxODI4ODkzNTgwMzAxMiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 9745e05e9721c04050f8dcaa8314e6e05838e323c03b0aa99f88778d98051ac4 |
| hovercard-subject-tag | issue:2198188005 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/patrickfav/bytes-java/65/issue_layout |
| twitter:image | https://opengraph.githubassets.com/585380464b3f162e7f5aee22f9368a2f29fb10a1808d7f351b1daca967150a23/patrickfav/bytes-java/issues/65 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/585380464b3f162e7f5aee22f9368a2f29fb10a1808d7f351b1daca967150a23/patrickfav/bytes-java/issues/65 |
| og:image:alt | When Base64.decode(CharSequence in) decodes a string, in some cases for "bad" strings in throws IllegalArgumentException: bytes-java/src/main/java/at/favre/lib/bytes/Base64.java Line 92 in e622b3b ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | xak2000 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4922b452d03cd8dbce479d866a11bc25b59ef6ee2da23aa9b0ddefa6bd4d0064 |
| turbo-cache-control | no-preview |
| go-import | github.com/patrickfav/bytes-java git https://github.com/patrickfav/bytes-java.git |
| octolytics-dimension-user_id | 2980711 |
| octolytics-dimension-user_login | patrickfav |
| octolytics-dimension-repository_id | 108965147 |
| octolytics-dimension-repository_nwo | patrickfav/bytes-java |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 108965147 |
| octolytics-dimension-repository_network_root_nwo | patrickfav/bytes-java |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 7e5ae23c70136152637ceee8d6faceb35596ec46 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width