Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Format error occurs when the specified width is less than the left aligned data length #2221

Open
F1F88 opened this issue Nov 17, 2024 · 2 comments

Comments

@F1F88
Copy link

F1F88 commented Nov 17, 2024

Description

If a specified width is smaller than the length of the actual data and left alignment is chosen, the formatted output erroneously appends extra characters until the buffer is filled.

Problematic Code (or Steps to Reproduce)

#include <sourcemod>

public void OnPluginStart()
{
    PrintToServer("Binary: (%-04b)", 1234567);
    PrintToServer("Unsigned Int: (%-04u)", 1234567);
    PrintToServer("Int: (%-04d)", 1234567);
    PrintToServer("Hex - 1: (%-04X)", 1234567);
    PrintToServer("Hex - 2: (%-04x)", 1234567);
}

Logs

Expected:

Binary: (100101101011010000111)
Unsigned Int: (1234567)
Int: (1234567)
Hex - 1: (12D687)
Hex - 2: (12d687)

Actual:

Binary: (1001011010110100001110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Unsigned Int: (1234567000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Int: (1234567000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Hex - 1: (12D687000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Hex - 2: (12d687000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
@F1F88
Copy link
Author

F1F88 commented Nov 17, 2024

I can open a pull request if needed.

@F1F88
Copy link
Author

F1F88 commented Nov 17, 2024

I think it is caused by the following lines of code not handling the width variable correctly:

It should be modified to:

while ((width-- > 0) && maxlen)

Just like here:

F1F88 added a commit to F1F88/sm-ext-log4sp that referenced this issue Dec 3, 2024
* 采用适配器设计模式重构 logger handle、sink handle
* 修复 Logger.Get() 可能泄露 handle 的问题
* 修复 Logger.AddSink(), Logger.DropSink(), Logger.SetErrorHandler() 的线程安全问题
* Logger.DropSink() 返回值由 bool -> void
* 重构 Logger 的错误处理器
     Logger.SetErrorHandler(Log4spErrorCallback callback); -> Logger.SetErrorHandler(LoggerErrorHandler handler);
     Log4spErrorCallback -> LoggerErrorHandler
     function void (const char[] msg); -> function void (Logger logger, const char[] msg);
* 恢复了 Logger.***AmxTpl() API
     与之前相同,依然使用 SM 提供的 format
     会中断 sourcepawn 代码
     长度超过 2048 会被截断
     格式化存在 BUG(alliedmodders/sourcemod#2221)
     但是性能应该稍快一点 (5~8%)
* 增强宏 LOG4SP_NO_EXT
* 更新注释文档
* 重命名 sink 构造函数参数 async -> multiThread
* 重构 Sink 过滤器 ClientChatSinkFilter -> SinkClientFilter
* 移除 Sink 多余的记录日志 API(只保留一个记录原始日志的API)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant