{"record":{"id":"3cd9533315a5c4e7","repo":"peass-ng/PEASS-ng","slug":"if-data-is-empty-padbits-must-be-0","errorCode":null,"errorMessage":"if 'data' is empty, 'padBits' must be 0","messagePattern":"if 'data' is empty, 'padBits' must be 0","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"winPEAS/winPEASexe/winPEAS/3rdParty/BouncyCastle/asn1/DerBitString.cs","lineNumber":81,"sourceCode":"            }\n\n            return FromAsn1Octets(((Asn1OctetString)o).GetOctets());\n        }\n\n        /**\n\t\t * @param data the octets making up the bit string.\n\t\t * @param padBits the number of extra bits at the end of the string.\n\t\t */\n        public DerBitString(\n            byte[] data,\n            int padBits)\n        {\n            if (data == null)\n                throw new ArgumentNullException(\"data\");\n            if (padBits < 0 || padBits > 7)\n                throw new ArgumentException(\"must be in the range 0 to 7\", \"padBits\");\n            if (data.Length == 0 && padBits != 0)\n                throw new ArgumentException(\"if 'data' is empty, 'padBits' must be 0\");\n\n            this.mData = Arrays.Clone(data);\n            this.mPadBits = padBits;\n        }\n\n        public DerBitString(\n            byte[] data)\n            : this(data, 0)\n        {\n        }\n\n        public DerBitString(\n            int namedBits)\n        {\n            if (namedBits == 0)\n            {\n                this.mData = new byte[0];\n                this.mPadBits = 0;","sourceCodeStart":63,"sourceCodeEnd":99,"githubUrl":"https://github.com/peass-ng/PEASS-ng/blob/53fb989abc2219826385683a6fee826bd6cd38d6/winPEAS/winPEASexe/winPEAS/3rdParty/BouncyCastle/asn1/DerBitString.cs#L63-L99","documentation":"A DER BIT STRING with an empty payload is only valid when there are no pad bits; if data is empty but padBits != 0 the encoding is contradictory (padding bits describing bits that do not exist). The constructor throws ArgumentException for this combination.","triggerScenarios":"Calling new DerBitString(new byte[0], padBits) with any padBits from 1 to 7.","commonSituations":"Encoding empty flags/bitmask values; code that trims a buffer to empty but keeps a previously computed padBits value.","solutions":["Pass padBits 0 when data is empty, or avoid constructing the string at all for empty input.","Guard the call: only compute/pass a nonzero padBits when data.Length > 0.","If the value is truly empty, use DerBitString.Empty or a null-typed ASN.1 object instead."],"exampleFix":"// before\nvar bs = new DerBitString(data, padBits); // data may be empty with padBits 5\n// after\nvar bs = data.Length == 0 ? new DerBitString(new byte[0], 0) : new DerBitString(data, padBits);","handlingStrategy":"validation","validationCode":"if (data.Length == 0 && padBits != 0) padBits = 0; // or reject the call","typeGuard":"static bool IsEncodableBitString(byte[] d, int pad) => d.Length > 0 || pad == 0;","tryCatchPattern":null,"preventionTips":["Reset padBits to 0 whenever the payload becomes empty","Skip creating DER objects for empty optional values","Add a precondition check in encoder wrappers"],"tags":["csharp","asn1","der-encoding","bouncycastle","empty-input"],"backgroundTag":"der-encoding-invalid","analyzedSha":"53fb989abc2219826385683a6fee826bd6cd38d6","analyzedAt":"2026-09-02T04:25:09.259Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T11:17:12.671Z"}