{"record":{"id":"c0c704cc93aa7165","repo":"Tencent/tinker","slug":"comment-too-long-bytes","errorCode":null,"errorMessage":"Comment too long: {} bytes","messagePattern":"Comment too long: (.+?) bytes","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"third-party/tinker-ziputils/src/main/java/com/tencent/tinker/ziputils/ziputil/AlignedZipOutputStream.java","lineNumber":397,"sourceCode":"        if (currentEntry.getExtra() != null) {\r\n            out.write(currentEntry.getExtra());\r\n        }\r\n        makePaddingToStream(out, padding);\r\n    }\r\n\r\n    /**\r\n     * Sets the comment associated with the file being written.\r\n     * @throws IllegalArgumentException if the comment is >= 64 Ki UTF-8 bytes.\r\n     */\r\n    public void setComment(String comment) {\r\n        if (comment == null) {\r\n            this.commentBytes = null;\r\n            return;\r\n        }\r\n\r\n        byte[] newCommentBytes = comment.getBytes(Charset.forName(\"UTF-8\"));\r\n        if (newCommentBytes.length > 0xffff) {\r\n            throw new IllegalArgumentException(\"Comment too long: \" + newCommentBytes.length + \" bytes\");\r\n        }\r\n        this.commentBytes = newCommentBytes;\r\n    }\r\n\r\n    /**\r\n     * Sets the <a href=\"Deflater.html#compression_level\">compression level</a> to be used\r\n     * for writing entry data.\r\n     */\r\n    public void setLevel(int level) {\r\n        if (level < Deflater.DEFAULT_COMPRESSION || level > Deflater.BEST_COMPRESSION) {\r\n            throw new IllegalArgumentException(\"Bad level: \" + level);\r\n        }\r\n        compressionLevel = level;\r\n    }\r\n\r\n    /**\r\n     * Sets the default compression method to be used when a {@code ZipEntry} doesn't\r\n     * explicitly specify a method. See {@link ZipEntry#setMethod} for more details.\r","sourceCodeStart":379,"sourceCodeEnd":415,"githubUrl":"https://github.com/Tencent/tinker/blob/1b7ea02c239840f563ea64fb5bd286eb98d4011e/third-party/tinker-ziputils/src/main/java/com/tencent/tinker/ziputils/ziputil/AlignedZipOutputStream.java#L379-L415","documentation":"The zip end-of-central-directory record stores the archive comment length in 16 bits, so a comment whose UTF-8 encoding is 65,536 bytes or more cannot be written. setComment encodes immediately and throws IllegalArgumentException when newCommentBytes.length > 0xffff, failing fast rather than producing a truncated comment field.","triggerScenarios":"Calling setComment with a very long string — e.g. embedding a JSON metadata blob, build manifest, or log excerpt as the zip comment.","commonSituations":"Build pipelines that stuff patch metadata (file lists, hashes, signatures) into the APK/zip comment and grow past 64Ki over time; multi-byte content (CJK) halving the effective character budget; copying a comment from another tool that allowed larger comments via Zip64 extensible records.","solutions":["Keep the comment under 65,535 UTF-8 bytes; enforce the limit where the comment string is assembled.","Move large metadata out of the comment into a dedicated zip entry (e.g. META-INF/metadata.json) with no size restrictions.","If the comment carries a signature/key block, shorten by hashing (store a digest plus a URL instead of full content)."],"exampleFix":"// before: stuffing arbitrary-length metadata into the comment\nzos.setComment(buildMetadataJson()); // grows past 64Ki over time\n\n// after: cap the comment and move bulk metadata to an entry\nbyte[] meta = buildMetadataJson();\nif (meta.length > 0xffff) {\n    zos.putNextEntry(new ZipEntry(\"META-INF/metadata.json\"));\n    zos.write(meta);\n    zos.closeEntry();\n    zos.setComment(\"see META-INF/metadata.json\");\n} else {\n    zos.setComment(new String(meta, StandardCharsets.UTF_8));\n}","handlingStrategy":"validation","validationCode":"// Validate comment size before calling setComment\nstatic String fitComment(String comment) {\n    if (comment == null) return null;\n    byte[] b = comment.getBytes(java.nio.charset.StandardCharsets.UTF_8);\n    if (b.length > 0xffff) throw new IllegalArgumentException(\"comment too long: \" + b.length + \" bytes\");\n    return comment;\n}","typeGuard":null,"tryCatchPattern":"try {\n    zos.setComment(comment);\n} catch (IllegalArgumentException e) {\n    if (e.getMessage() != null && e.getMessage().startsWith(\"Comment too long\")) {\n        zos.setComment(comment.substring(0, comment.length() / 2)); // or move to an entry\n    } else {\n        throw e;\n    }\n}","preventionTips":["Keep zip comments to short identifiers; store bulk metadata as an entry.","Size-check any generated comment against 65,535 UTF-8 bytes.","Watch multi-byte content when computing the budget."],"tags":["zip","comment-length","utf8","tinker"],"backgroundTag":null,"analyzedSha":"1b7ea02c239840f563ea64fb5bd286eb98d4011e","analyzedAt":"2026-08-14T15:16:52.110Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}