{"record":{"id":"fe6150a33a56ff66","repo":"zloirock/core-js","slug":"malformed-padding-exactly-one-additional-characte","errorCode":null,"errorMessage":"Malformed padding: exactly one additional character","messagePattern":"Malformed padding: exactly one additional character","errorType":"exception","errorClass":"SyntaxError","httpStatus":null,"severity":"error","filePath":"packages/core-js/internals/uint8-from-base64.js","lineNumber":102,"sourceCode":"  if (into) notDetached(into.buffer);\n\n  var stringLength = string.length;\n  var bytes = into || $Array(floor(stringLength * 3 / 4));\n  var written = 0;\n  var read = 0;\n  var chunk = '';\n  var index = 0;\n\n  if (maxLength) while (true) {\n    index = skipAsciiWhitespace(string, index);\n    if (index === stringLength) {\n      if (chunk.length > 0) {\n        if (lastChunkHandling === 'stop-before-partial') {\n          break;\n        }\n        if (lastChunkHandling === 'loose') {\n          if (chunk.length === 1) {\n            throw new SyntaxError('Malformed padding: exactly one additional character');\n          }\n          written = writeBytes(bytes, decodeBase64Chunk(chunk, alphabet, false), written);\n        } else {\n          throw new SyntaxError('Missing padding');\n        }\n      }\n      read = stringLength;\n      break;\n    }\n    var chr = at(string, index);\n    ++index;\n    if (chr === '=') {\n      if (chunk.length < 2) {\n        throw new SyntaxError('Padding is too early');\n      }\n      index = skipAsciiWhitespace(string, index);\n      if (chunk.length === 2) {\n        if (index === stringLength) {","sourceCodeStart":84,"sourceCodeEnd":120,"githubUrl":"https://github.com/zloirock/core-js/blob/84e45fba098dd3a177d5cf2247d06ab8e98d3790/packages/core-js/internals/uint8-from-base64.js#L84-L120","documentation":"When decoding reaches the end of the input with a leftover partial chunk, 'loose' lastChunkHandling accepts a 2- or 3-character final chunk (without padding) but rejects a single leftover character, because one base64 char carries only 6 bits — not enough to encode any byte. This SyntaxError reports that exact case.","triggerScenarios":"`Uint8Array.fromBase64('QQ A'.replace(/\\s/g,''))`-style input whose final group is exactly 1 character, e.g. 'QUIx' (4+... wait) — concretely a string like 'QQB'+'j' where the last chunk after splitting into 4s is 'j'. Also whitespace-split strings that lost characters.","commonSituations":"Base64 strings truncated by one character during copy/paste or storage trimming, custom encoders emitting a lone trailing char, or regex/split processing that dropped '=' padding leaving one char.","solutions":["Fix the source data — a single trailing base64 char is never valid; the string is truncated or corrupt.","Use lastChunkHandling: 'stop-before-partial' to decode only up to the last complete chunk.","Add missing padding: a 1-char tail cannot be padded to validity, so repair/re-request the data.","Validate `string.length % 4 !== 1` before decoding."],"exampleFix":"// before\nUint8Array.fromBase64('QQBj'); // chunks 'QQBj' ok — but 'QQBjx' leaves chunk 'x' -> SyntaxError\n// after\nconst s = b64.replace(/\\s/g, '');\nif (s.length % 4 === 1) throw new Error('truncated base64');\nUint8Array.fromBase64(s);","handlingStrategy":"validation","validationCode":"const s = b64.replace(/\\s/g, '');\nif (s.length % 4 === 1) {\n  throw new Error('base64 input truncated: single trailing character cannot encode any byte');\n}","typeGuard":"function isDecodableBase64Length(s) { return s.replace(/\\s/g, '').length % 4 !== 1; }","tryCatchPattern":"try {\n  bytes = Uint8Array.fromBase64(b64);\n} catch (e) {\n  if (e instanceof SyntaxError && /Malformed padding/.test(e.message)) {\n    bytes = Uint8Array.fromBase64(b64, { lastChunkHandling: 'stop-before-partial' });\n  } else throw e;\n}","preventionTips":["Check that stripped base64 length % 4 !== 1 before decoding.","Investigate any 1-char tail as data corruption/truncation — don't try to 'fix' it silently.","Use 'stop-before-partial' when consuming streaming chunks of unknown completeness."],"tags":["core-js","base64","padding","syntaxerror"],"backgroundTag":"base64-malformed-padding","analyzedSha":"84e45fba098dd3a177d5cf2247d06ab8e98d3790","analyzedAt":"2026-08-30T20:36:10.323Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}