一句话:提出 SUDS 指标与 Dual Reasoning 推理时方法,在代码生成前做安全审计与审查,统一功能正确性与自然语言安全警告。
现有 Code LLM 仅以功能正确性为评估标准,忽视生成代码是否传播提示中注入的有害内容;已有方法多聚焦有害内容检测,却忽略功能正确性。本文基于双重信道约束理论(代码同时具有面向机器执行的算法信道与面向人类交流的自然语言信道,因而存在独特的安全–效用权衡),提出 NLSafety-Utility Duality Score(SUDS),在 12 种排序响应场景下把代码效用、安全遵循与警告意识统一为单一分数;并提出 Dual Reasoning(DR),一种要求在生成代码前显式进行安全审计与任务相关代码审查的结构化推理时技术。在 5 个 LLM 与 2 个注入有害关键词的基准(820 与 2135 个样本)上,DR 在所有模型上均取得最高 SUDS,较基线平均提升 1.32×–3.42×;而思维链提示的安全增益微乎其微,安全感知提示仅有部分改善。进一步分析表明 DR 的效果随模型容量扩展,one-shot 示例主要用于稳定小模型的输出格式,结构化推理无法弥补安全词表有限的模型。
Large language models (LLMs) for code generation are typically evaluated on functional correctness alone, overlooking whether generated code propagates harmful content embedded in the prompt. Prior work has shown that most Code LLMs reproduce offensive identifiers from injected renaming instructions without warning, yet existing approaches focus on detecting harmful content, neglecting functional correctness. Grounded in the Theory of Dual Channel Constraints (which states that code is a dual-channel medium combining an algorithmic (AL) channel for machine execution and a natural language (NL) channel for human communication, creating a unique safety-utility trade-off where a model must balance functional execution with responsible communication), we propose NLSafety-Utility Duality Score (SUDS), a metric that unifies code utility, safety adherence, and warning awareness into a single score across 12 ranked response scenarios, and Dual Reasoning (DR), a structured inference-time technique that requires an explicit safety audit and task-grounded code review before code generation. Evaluated on five LLMs across two benchmarks augmented with harmful keyword injections (820 and 2,135 samples), DR consistently achieves the highest SUDS across all models, improving mean SUDS by 1.32× to 3.42× over the baseline, while chain-of-thought prompting yields negligible safety gains and a safety-aware prompt provides only partial improvement. Further analysis reveals that DR's effectiveness scales with model capacity, that the one-shot exemplar primarily stabilizes output format for smaller models, and that structured reasoning cannot compensate for models with limited safety vocabularies.