int32 len = va_args(va, int32) 在 x86_64 GNU/Linux 上给出非常大的值

发布于 2024-11-25 03:11:43 字数 3968 浏览 3 评论 0原文

void AppBuf(message_id_type msgID, int32 numPairs, va_list va)

{
int32 len = va_args(va, int32);
....
}

上面的代码在 Windows(32 和 64 位)以及 Linux 32 位编译器上运行得非常好。 对于以上所有情况,“len”的值均为 10。

但在 linux 64 位 (x86_64 GNU/Linux) 上,我得到了一个非常大的 len 值 (50462976),这会弄乱其余的代码并最终导致一场车祸。

我读到 Linux 64 位编译器中关于 va_lists 的某些内容发生了变化,但我无法理解这些变化,因此无法解决我的问题。

有人可以帮我解决这个问题吗?

谢谢。

Sunny

好的,以下是到目前为止的整个代码的详细信息: 有人可以帮忙解决这个问题吗?提前致谢。

调用堆栈:

AppendBuffers(int msgID=0x00000001, int numPairs=0x00000001, char * va=0x0007fcb0) {注意:如上所述,这就是问题发生的地方(长度=非常大) }

LogMsg(int msgID=0x00000001, int numPairs=0x00000001,char * 参数=0x0007fcb0)

LogMsgBuffersV(int msgID=0x00000001,int numPairs=0x00000001,char * 参数=0x0007fcb0)

LogMsgBuffersV(int msgID=0x00000001,int numPairs=0x00000001, char * argument=0x0007fcb0)

LogMsgBuffers(int msgID=0x00000001, int numPairs=0x00000001, ...)

实际代码:

void LogMsgBuffers(message_id_type msgID, int32 numPairs, ...)
{

    va_list arguments;
    va_start(arguments, numPairs);

    filter_status_type msgStatus = FilterMsg(msgID);

    if (msgStatus == FILTER_ACCEPT)
    {
        LogMsg(msgID, numPairs, arguments);
    }

    if ((_parentLogger != NULL) && (_oAppenderInheritance))
    {
        //Pass the msg to the parent
        _parentLogger->LogMsgBuffersV(msgID, numPairs, arguments);
    }

    return;
}

void LogMsgBuffersV(message_id_type msgID, int32 numPairs, va_list arguments)
{

    filter_status_type msgStatus = FilterMsg(msgID);

    if (msgStatus == FILTER_ACCEPT)
    {
        //Log msg to the current node
        LogMsg(msgID, numPairs, arguments);
    }

    if ((_parentLogger != NULL) && (_oAppenderInheritance))
    {
        //Pass the msg to the parent
        _parentLogger->LogMsgBuffersV(msgID, numPairs, arguments);

    }
    return;
}

void LogMsgBuffersV(message_id_type msgID, int32 numPairs, va_list arguments)
{
    filter_status_type msgStatus = FilterMsg(msgID);

    if (msgStatus == FILTER_ACCEPT)
    {
        //Log msg to the current node
        LogMsg(msgID, numPairs, arguments);
    }

    if ((_parentLogger != NULL) && (_oAppenderInheritance))
    {
        //Pass the msg to the parent
        _parentLogger->LogMsgBuffersV(msgID, numPairs, arguments);

    }
    return;
}

void LogMsg(message_id_type msgID, int32 numPairs, va_list arguments)
{
    uint32 i;

    for (i = 0; i < _pOwnAppenderVec.size(); i++)
    {
        LoggerAppender *appender = _pOwnAppenderVec[i];
        appender->AppendBuffers(msgID, numPairs, arguments);
    }
    return;
}

void AppendBuffers(message_id_type msgID, int32 numPairs, va_list va)
        {

            for (int32 i = 0; i < numPairs; i++)
            {
                int32 length = va_arg(va, int32);
                uint8* buffer = va_arg(va, uint8*);

                int32 jj;
                for (jj = 10; jj < length; jj += 10)
                {
                    AppendStringA(0, "  %x %x %x %x %x %x %x %x %x %x", buffer[0], buffer[1], buffer[2], buffer[3], buffer[4], buffer[5], buffer[6], buffer[7], buffer[8], buffer[9]);
                    buffer += 10;
                }

                uint8 remainderbuf[10];
                uint32 remainder = length - (jj - 10);
                if (remainder > 0 && remainder <= 10)
                {
                    oscl_memcpy(remainderbuf, buffer, remainder);
                    oscl_memset(remainderbuf + remainder, 0, 10 - remainder);
                    buffer = remainderbuf;
                    AppendStringA(0, "  %x %x %x %x %x %x %x %x %x %x", buffer[0], buffer[1], buffer[2], buffer[3], buffer[4], buffer[5], buffer[6], buffer[7], buffer[8], buffer[9]);
                }
            }
            va_end(va);
        }
void AppBuf(message_id_type msgID, int32 numPairs, va_list va)

{
int32 len = va_args(va, int32);
....
}

The above piece of code runs perfectly fine on windows (32 and 64 bit) and also on linux 32 bit compiler.
Value of 'len' comes out to 10 for all the above.

But on linux 64 bit (x86_64 GNU/Linux) I am getting a very large value for len (50462976), which messes up the rest of the code and end ups in a crash.

I had read that something has changed in linux 64 bit compilers with respect to va_lists but I was not able to understand the change and so I was not able to fix my issue.

Could someone please help me with this issue?

Thanks.

Sunny

Ok, here are the details of the whole code leading upto this point:
Could someone please help with this? Thanks in advance.

Call Stack:

AppendBuffers(int msgID=0x00000001, int numPairs=0x00000001, char * va=0x0007fcb0) {NOTE: this is where the problem is occuring as mentioned above (length = very large)}

LogMsg(int msgID=0x00000001, int numPairs=0x00000001, char * arguments=0x0007fcb0)

LogMsgBuffersV(int msgID=0x00000001, int numPairs=0x00000001, char * arguments=0x0007fcb0)

LogMsgBuffersV(int msgID=0x00000001, int numPairs=0x00000001, char * arguments=0x0007fcb0)

LogMsgBuffers(int msgID=0x00000001, int numPairs=0x00000001, ...)

Actual code:

void LogMsgBuffers(message_id_type msgID, int32 numPairs, ...)
{

    va_list arguments;
    va_start(arguments, numPairs);

    filter_status_type msgStatus = FilterMsg(msgID);

    if (msgStatus == FILTER_ACCEPT)
    {
        LogMsg(msgID, numPairs, arguments);
    }

    if ((_parentLogger != NULL) && (_oAppenderInheritance))
    {
        //Pass the msg to the parent
        _parentLogger->LogMsgBuffersV(msgID, numPairs, arguments);
    }

    return;
}

void LogMsgBuffersV(message_id_type msgID, int32 numPairs, va_list arguments)
{

    filter_status_type msgStatus = FilterMsg(msgID);

    if (msgStatus == FILTER_ACCEPT)
    {
        //Log msg to the current node
        LogMsg(msgID, numPairs, arguments);
    }

    if ((_parentLogger != NULL) && (_oAppenderInheritance))
    {
        //Pass the msg to the parent
        _parentLogger->LogMsgBuffersV(msgID, numPairs, arguments);

    }
    return;
}

void LogMsgBuffersV(message_id_type msgID, int32 numPairs, va_list arguments)
{
    filter_status_type msgStatus = FilterMsg(msgID);

    if (msgStatus == FILTER_ACCEPT)
    {
        //Log msg to the current node
        LogMsg(msgID, numPairs, arguments);
    }

    if ((_parentLogger != NULL) && (_oAppenderInheritance))
    {
        //Pass the msg to the parent
        _parentLogger->LogMsgBuffersV(msgID, numPairs, arguments);

    }
    return;
}

void LogMsg(message_id_type msgID, int32 numPairs, va_list arguments)
{
    uint32 i;

    for (i = 0; i < _pOwnAppenderVec.size(); i++)
    {
        LoggerAppender *appender = _pOwnAppenderVec[i];
        appender->AppendBuffers(msgID, numPairs, arguments);
    }
    return;
}

void AppendBuffers(message_id_type msgID, int32 numPairs, va_list va)
        {

            for (int32 i = 0; i < numPairs; i++)
            {
                int32 length = va_arg(va, int32);
                uint8* buffer = va_arg(va, uint8*);

                int32 jj;
                for (jj = 10; jj < length; jj += 10)
                {
                    AppendStringA(0, "  %x %x %x %x %x %x %x %x %x %x", buffer[0], buffer[1], buffer[2], buffer[3], buffer[4], buffer[5], buffer[6], buffer[7], buffer[8], buffer[9]);
                    buffer += 10;
                }

                uint8 remainderbuf[10];
                uint32 remainder = length - (jj - 10);
                if (remainder > 0 && remainder <= 10)
                {
                    oscl_memcpy(remainderbuf, buffer, remainder);
                    oscl_memset(remainderbuf + remainder, 0, 10 - remainder);
                    buffer = remainderbuf;
                    AppendStringA(0, "  %x %x %x %x %x %x %x %x %x %x", buffer[0], buffer[1], buffer[2], buffer[3], buffer[4], buffer[5], buffer[6], buffer[7], buffer[8], buffer[9]);
                }
            }
            va_end(va);
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟火散人牵绊 2024-12-02 03:11:43

AFACIT,va_args 不是标准函数。事实上,除了 __VA_ARGS__ 宏之外,Google 还将此问题作为其最佳答案之一。

事实上,没有标准方法可以确定给定 va_list 中有多少个 va_arg 参数,并且 GCC 页面 也没有列出任何方法。

AFACIT, va_args is not a standard function. In fact, Google is turning up this question as one of its top answers, besides __VA_ARGS__ macros.

There's in fact no standard way to determine how many va_arg arguments are in a given va_list, and the GCC page doesn't list any method either.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文