{"record":{"id":"db8bb42b5189c3bd","repo":"eclipse-vertx/vert.x","slug":"cannot-truncate-file-to-size-0","errorCode":null,"errorMessage":"Cannot truncate file to size < 0","messagePattern":"Cannot truncate file to size < 0","errorType":"exception","errorClass":"FileSystemException","httpStatus":null,"severity":"error","filePath":"vertx-core/src/main/java/io/vertx/core/file/impl/FileSystemImpl.java","lineNumber":501,"sourceCode":"          Path source = resolveFile(from).toPath();\n          Path target = resolveFile(to).toPath();\n          Files.move(source, target, copyOptions);\n        } catch (IOException e) {\n          throw new FileSystemException(getFileMoveErrorMessage(from, to), e);\n        }\n        return null;\n      }\n    };\n  }\n\n  private BlockingAction<Void> truncateInternal(String p, long len) {\n    Objects.requireNonNull(p);\n    return new BlockingAction<Void>() {\n      public Void perform() {\n        try {\n          String path = resolveFile(p).getAbsolutePath();\n          if (len < 0) {\n            throw new FileSystemException(\"Cannot truncate file to size < 0\");\n          }\n          if (!Files.exists(Paths.get(path))) {\n            throw new FileSystemException(\"Cannot truncate file \" + path + \". Does not exist\");\n          }\n          try (RandomAccessFile raf = new RandomAccessFile(path, \"rw\")) {\n            raf.setLength(len);\n          }\n        } catch (IOException e) {\n          throw new FileSystemException(getFileAccessErrorMessage(\"truncate\", p) ,e);\n        }\n        return null;\n      }\n    };\n  }\n\n  private BlockingAction<Void> chmodInternal(String path, String perms) {\n    return chmodInternal(path, perms, null);\n  }","sourceCodeStart":483,"sourceCodeEnd":519,"githubUrl":"https://github.com/eclipse-vertx/vert.x/blob/fb308bd8c3f12c79f4ae89bef67fadf6c80d036e/vertx-core/src/main/java/io/vertx/core/file/impl/FileSystemImpl.java#L483-L519","documentation":"The truncate action in FileSystemImpl validates the requested length before touching the file and throws FileSystemException 'Cannot truncate file to size < 0' when a negative size is passed. Vert.x truncates via RandomAccessFile.setLength, which only accepts non-negative lengths, so the library fails fast with a clear message. This is a caller input-validation error, not an I/O failure.","triggerScenarios":"vertx.fileSystem().truncate(path, len) or truncateBlocking with a negative len — typically a computed size such as fileLength - bytesRemoved where the subtraction went negative.","commonSituations":"Truncating to 'current size minus offset' when the offset exceeds the file size; parsing a size from config/CLI where a negative slips through; integer overflow producing a negative value.","solutions":["Clamp or validate the target length: if (len < 0) len = 0; or reject the input before calling truncate.","Recompute the intended size — verify the offset/bytes-to-remove against the actual file size (fs.props(path).size()).","If the size comes from configuration, validate it is >= 0 at load time.","If you intended to empty the file, pass 0 explicitly."],"exampleFix":"// before\nlong len = props.size() - consumed; // may be negative\nvertx.fileSystem().truncateBlocking(path, len);\n// after\nlong len = Math.max(0, props.size() - consumed);\nvertx.fileSystem().truncateBlocking(path, len);","handlingStrategy":"validation","validationCode":"long len = computeTargetSize();\nif (len < 0) throw new IllegalArgumentException(\"truncate size must be >= 0, got \" + len);\nvertx.fileSystem().truncateBlocking(path, len);","typeGuard":"static Long nonNegativeSize(Long size) {\n  return (size == null || size < 0) ? 0L : size;\n}","tryCatchPattern":"try {\n  fs.truncateBlocking(path, len);\n} catch (FileSystemException e) {\n  if (e.getMessage().contains(\"size < 0\")) {\n    fs.truncateBlocking(path, 0); // or reject the request upstream\n  } else throw e;\n}","preventionTips":["Validate any size read from config/CLI with a >= 0 check","Compute sizes with Math.max(0, size - offset)","Watch for integer overflow when subtracting offsets from file sizes","Use 0 explicitly when the intent is to empty the file"],"tags":["file-system","truncate","validation","argument"],"backgroundTag":"invalid-argument-value","analyzedSha":"fb308bd8c3f12c79f4ae89bef67fadf6c80d036e","analyzedAt":"2026-09-06T11:37:12.241Z","contentChangedAt":"2026-09-06T11:37:12.241Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}