Skip to content

Commit 93bd732

Browse files
committed
fix: catch NumberFormatException on CLI numeric options (CodeQL #269-273)
OpenAiCompatServer's CLI main() called Integer.parseInt on --ctx, --gpu-layers, --parallel and --port without guarding NumberFormatException, so a non-numeric value (e.g. "--port abc") crashed with a raw stack trace — flagged by CodeQL as "missing catch of NumberFormatException" (5 alerts). Consolidate the numeric parsing into one try/catch that prints a clear message and returns (no System.exit — the noSystemExit architecture rule forbids it; this mirrors the existing missing-"--model" usage path), and parse --ctx once instead of twice. Verified: compiles clean under -Werror / NullAway / Checker; LlamaArchitectureTest (noSystemExit) passes. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014L2dLbAtwdq7C6a2gFRsQQ
1 parent cf3e487 commit 93bd732

1 file changed

Lines changed: 30 additions & 21 deletions

File tree

src/main/java/net/ladenthin/llama/server/OpenAiCompatServer.java

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -352,28 +352,12 @@ public static void main(String[] args) throws IOException {
352352
}
353353

354354
ModelParameters modelParams = new ModelParameters().setModel(modelPath);
355-
String ctx = opts.get("ctx");
356-
if (ctx != null) {
357-
modelParams.setCtxSize(Integer.parseInt(ctx));
358-
}
359-
String gpuLayers = opts.get("gpu-layers");
360-
if (gpuLayers != null) {
361-
modelParams.setGpuLayers(Integer.parseInt(gpuLayers));
362-
}
363-
String parallel = opts.get("parallel");
364-
if (parallel != null) {
365-
modelParams.setParallel(Integer.parseInt(parallel));
366-
}
367-
368355
OpenAiServerConfig.Builder cfg = OpenAiServerConfig.builder();
356+
369357
String host = opts.get("host");
370358
if (host != null) {
371359
cfg.host(host);
372360
}
373-
String port = opts.get("port");
374-
if (port != null) {
375-
cfg.port(Integer.parseInt(port));
376-
}
377361
String apiKey = opts.get("api-key");
378362
if (apiKey != null) {
379363
cfg.apiKey(apiKey);
@@ -382,11 +366,36 @@ public static void main(String[] args) throws IOException {
382366
if (modelId != null) {
383367
cfg.modelId(modelId);
384368
}
385-
if (ctx != null) {
386-
int ctxSize = Integer.parseInt(ctx);
387-
cfg.maxOutputTokens(Math.min(OpenAiServerConfig.DEFAULT_MAX_OUTPUT_TOKENS, Math.max(1, ctxSize / 2)));
388-
cfg.maxInputTokens(Math.max(1, ctxSize - OpenAiServerConfig.DEFAULT_MAX_OUTPUT_TOKENS));
369+
370+
// Parse all numeric options in one place so a non-numeric value (e.g. "--port abc") yields a
371+
// clear message instead of an uncaught NumberFormatException stack trace. No System.exit here
372+
// — the noSystemExit architecture rule forbids it; print to stderr and return like the
373+
// missing-"--model" path above.
374+
try {
375+
String ctx = opts.get("ctx");
376+
if (ctx != null) {
377+
int ctxSize = Integer.parseInt(ctx);
378+
modelParams.setCtxSize(ctxSize);
379+
cfg.maxOutputTokens(Math.min(OpenAiServerConfig.DEFAULT_MAX_OUTPUT_TOKENS, Math.max(1, ctxSize / 2)));
380+
cfg.maxInputTokens(Math.max(1, ctxSize - OpenAiServerConfig.DEFAULT_MAX_OUTPUT_TOKENS));
381+
}
382+
String gpuLayers = opts.get("gpu-layers");
383+
if (gpuLayers != null) {
384+
modelParams.setGpuLayers(Integer.parseInt(gpuLayers));
385+
}
386+
String parallel = opts.get("parallel");
387+
if (parallel != null) {
388+
modelParams.setParallel(Integer.parseInt(parallel));
389+
}
390+
String port = opts.get("port");
391+
if (port != null) {
392+
cfg.port(Integer.parseInt(port));
393+
}
394+
} catch (NumberFormatException e) {
395+
System.err.println("Invalid numeric option (expected an integer): " + e.getMessage());
396+
return;
389397
}
398+
390399
OpenAiServerConfig config = cfg.build();
391400

392401
LlamaModel model = new LlamaModel(modelParams);

0 commit comments

Comments
 (0)