{"record":{"id":"af76ea75c24d086f","repo":"denoland/deno","slug":"err-socket-bad-buffer-size","errorCode":"ERR_SOCKET_BAD_BUFFER_SIZE","errorMessage":"Buffer size must be a positive integer","messagePattern":"Buffer size must be a positive integer","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"ext/node/polyfills/dgram.ts","lineNumber":1403,"sourceCode":"\nfunction replaceHandle(self: Socket, newHandle: UDP) {\n  const state = self[kStateSymbol];\n  const oldHandle = state.handle!;\n\n  // Set up the handle that we got from primary.\n  newHandle.lookup = oldHandle.lookup;\n  newHandle.bind = oldHandle.bind;\n  newHandle.send = oldHandle.send;\n  newHandle[ownerSymbol] = self;\n\n  // Replace the existing handle by the handle we got from primary.\n  oldHandle.close();\n  state.handle = newHandle;\n}\n\nfunction bufferSize(self: Socket, size: number, buffer: boolean): number {\n  if (size >>> 0 !== size) {\n    throw new ERR_SOCKET_BAD_BUFFER_SIZE();\n  }\n\n  const ctx = {};\n  const ret = self[kStateSymbol].handle!.bufferSize(size, buffer, ctx);\n\n  if (ret === undefined) {\n    throw new ERR_SOCKET_BUFFER_SIZE(ctx as NodeSystemErrorCtx);\n  }\n\n  return ret;\n}\n\nfunction socketCloseNT(self: Socket) {\n  self.emit(\"close\");\n}\n\nfunction healthCheck(socket: Socket) {\n  if (!socket[kStateSymbol].handle) {","sourceCodeStart":1385,"sourceCodeEnd":1421,"githubUrl":"https://github.com/denoland/deno/blob/89f33cbef296a2b287f323d42de54c871fa69c77/ext/node/polyfills/dgram.ts#L1385-L1421","documentation":"Backing validation for dgram Socket.setRecvBufferSize/setSendBufferSize (via the internal bufferSize() helper): the size is coerced with 'size >>> 0' and must round-trip exactly, i.e. be a uint32. Negative numbers, fractional values, NaN, Infinity, and anything above 0xFFFFFFFF fail the check and throw ERR_SOCKET_BAD_BUFFER_SIZE.","triggerScenarios":"socket.setRecvBufferSize(65536.5); socket.setSendBufferSize(-1); socket.setRecvBufferSize(NaN) (e.g. a failed parseInt); setRecvBufferSize(2 ** 33); passing a string like '65536'.","commonSituations":"Reading buffer sizes from config/env without numeric validation; computing sizes with division or scaling that yields fractions; parseInt returning NaN on malformed input.","solutions":["Sanitize to a non-negative integer before calling: Math.max(0, Math.floor(Number(size))) and verify !Number.isNaN","Use Number.isInteger(size) && size >= 0 && size <= 0xFFFFFFFF as a guard for values from untrusted sources","Pick a sane default (e.g. 4096..262144) when the configured value fails validation"],"exampleFix":"// before\nconst size = parseInt(process.env.SO_RCVBUF); // NaN if unset/malformed\nsock.setRecvBufferSize(size); // throws ERR_SOCKET_BAD_BUFFER_SIZE\n\n// after\nconst size = Number.parseInt(process.env.SO_RCVBUF, 10);\nsock.setRecvBufferSize(Number.isInteger(size) && size > 0 ? size : 65536);","handlingStrategy":"validation","validationCode":"function safeBufferSize(v, fallback = 65536) {\n  const n = Math.floor(Number(v));\n  return Number.isInteger(n) && n >= 0 && n <= 0xFFFFFFFF ? n : fallback;\n}\nsock.setRecvBufferSize(safeBufferSize(config.recvBuf));","typeGuard":"function isU32(v) { return typeof v === 'number' && Number.isInteger(v) && v >= 0 && v <= 0xFFFFFFFF; }","tryCatchPattern":null,"preventionTips":["Never feed parseInt results directly to buffer-size setters","Round fractional computed sizes with Math.floor","Keep configured sizes within a small known set of tested values"],"tags":["node-compat","dgram","udp","validation"],"backgroundTag":null,"analyzedSha":"89f33cbef296a2b287f323d42de54c871fa69c77","analyzedAt":"2026-08-16T07:54:21.310Z","schemaVersion":2},"datasetVersion":"2026-08-16T08:17:34.114Z"}