{"record":{"id":"1356734e3707c569","repo":"nilaoda/N_m3u8DL-RE","slug":"nonce-is-null","errorCode":null,"errorMessage":"Nonce is null","messagePattern":"Nonce is null","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/N_m3u8DL-RE/Crypto/CSChaCha20.cs","lineNumber":167,"sourceCode":"            state[3] = Util.U8To32Little(constants, 12);\n        }\n\n        /// <summary>\n        /// Set up the ChaCha state with the given nonce (aka Initialization Vector or IV) and block counter. A 12-byte nonce and a 4-byte counter are required.\n        /// </summary>\n        /// <param name=\"nonce\">\n        /// A 12-byte (96-bit) nonce, treated as a concatenation of three 32-bit little-endian integers\n        /// </param>\n        /// <param name=\"counter\">\n        /// A 4-byte (32-bit) block counter, treated as a 32-bit little-endian integer\n        /// </param>\n        private void IvSetup(byte[] nonce, uint counter)\n        {\n            if (nonce == null)\n            {\n                // There has already been some state set up. Clear it before exiting.\n                Dispose();\n                throw new ArgumentNullException(\"Nonce is null\");\n            }\n\n            if (nonce.Length != allowedNonceLength)\n            {\n                // There has already been some state set up. Clear it before exiting.\n                Dispose();\n                throw new ArgumentException($\"Nonce length must be {allowedNonceLength}. Actual: {nonce.Length}\");\n            }\n\n            state[12] = counter;\n            state[13] = Util.U8To32Little(nonce, 0);\n            state[14] = Util.U8To32Little(nonce, 4);\n            state[15] = Util.U8To32Little(nonce, 8);\n        }\n\n\n        #region Encryption methods\n","sourceCodeStart":149,"sourceCodeEnd":185,"githubUrl":"https://github.com/nilaoda/N_m3u8DL-RE/blob/e113dee70c924ee08dae5460624909b04d84cb76/src/N_m3u8DL-RE/Crypto/CSChaCha20.cs#L149-L185","documentation":"IvSetup validates the ChaCha20 nonce. A null nonce is rejected with ArgumentNullException(\"Nonce is null\"), and because some cipher state may already be set, the instance is Dispose()d before throwing to avoid a half-initialized cipher.","triggerScenarios":"Calling the ChaCha20 constructor/entry with nonce == null — e.g. the manifest omitted the IV, or the code path that derives the nonce returned null.","commonSituations":"Streams without an explicit IV where the caller forgot to supply the default/zero nonce; failed Base64 decode of the IV attribute yielding null.","solutions":["Check nonce != null before constructing the cipher.","Default to an all-zero 12-byte nonce when the stream provides none, if protocol-appropriate.","Fix the IV extraction code to return an empty/zero buffer rather than null.","Since state is disposed on failure, always construct a fresh cipher instance after fixing the input."],"exampleFix":"// before\nvar cipher = new CSChaCha20(key, nonceBytes, 0); // nonceBytes null\n// after\nnonceBytes ??= new byte[12];\nvar cipher = new CSChaCha20(key, nonceBytes, 0);","handlingStrategy":"type-guard","validationCode":"if (nonceBytes == null) nonceBytes = new byte[12]; // or abort","typeGuard":"bool HasNonce(byte[] n) => n is { Length: 12 };","tryCatchPattern":"try { cipher = new CSChaCha20(key, nonce, 0); }\ncatch (ArgumentNullException) { /* nonce missing — supply default or abort */ }","preventionTips":["Default to a zero 12-byte nonce when the manifest has no IV.","Check IV extraction results for null before decryption.","Remember the failed instance is disposed — always build a new cipher after fixing input."],"tags":["crypto","chacha20","null-check"],"backgroundTag":"null-argument","analyzedSha":"e113dee70c924ee08dae5460624909b04d84cb76","analyzedAt":"2026-09-13T02:43:21.078Z","contentChangedAt":"2026-09-13T02:43:21.078Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}