va_args不接受C中的签名整数
我正在使用C设计内核。在制作kprintf
函数(printf
之类的函数)时,我看到了签名的整数(精确的数据类型是<代码> long ),va_args
将它们转换为无签名的长
。
这是代码的片段: kprint.c
#include <stdarg.h>
// skipped some lines not needed for this question
// ...
/******************
* global variables needed for number to string conversion
* ***************/
// store the converted string temporarily
static char convertedString[30];
// store the index of the filled portion
static char numberIndex;
// TODO: Create function like printf but which works here
_Bool kprintf(const char* stringFormat, ...) {
char* strPtr = (char*)stringFormat;
va_list arguments;
va_start(arguments, stringFormat);
while (*strPtr != 0) {
switch (*strPtr)
{
case '\e':
// printf() also ends the execution
return 1;
case '%':
switch (*(strPtr + 1))
{
// signed decimal integer
case 'd':
case 'i':
kprintf(IntToString(va_arg(arguments, long long))); // stringify the integer
// *problem* here
strPtr += 2;
break;
// unsigned decimal integer
case 'u':
kprintf(uIntToString(va_arg(arguments, uint64_t))); // stringify
strPtr += 2;
break;
// will implement U & L case later
// unsigned hexadecimal
case 'x':
case 'X':
kprintf(uHexToString(va_arg(arguments, uint64_t))); // stringify
// doesn't work now
strPtr += 2;
break;
// will implement U & L case later
// 6 numbers after decimal
case 'f':
case 'F':
kprintf(DoubleToString(va_arg(arguments, double), 6)); // stringify
// doesn't work now
strPtr += 2;
break;
// 2 numbers after pt
case 'g':
case 'G':
kprintf(DoubleToString(va_arg(arguments, double), 2));
strPtr += 2;
break;
case 'c':
kPrintChar((char)va_arg(arguments, int));
//
strPtr += 2;
break;
// another string
case 's':
kPrintfHelper(va_arg(arguments, char*)); // just to prevent recursion
strPtr += 2;
break;
case '%':
kPrintChar('%');
strPtr += 2;
break;
}
break;
default:
kPrintChar(*strPtr);
strPtr++;
}
}
va_end(arguments);
return 0;
}
void uIntToStrHelper(uint64_t num) {
if (num < 10)
{
convertedString[numberIndex++] = num + '0';
convertedString[numberIndex] = 0;
return;
}
uint8_t numIndexCpy = numberIndex;
while (num > 0) {
convertedString[numberIndex++] = (num % 10) + '0';
num /= 10;
}
char swpIndex = (numberIndex - 2 + numIndexCpy) / 2;
numberIndex = numberIndex - swpIndex - 1 + numIndexCpy;
while (swpIndex >= numIndexCpy) {
convertedString[swpIndex] = convertedString[swpIndex] +
convertedString[numberIndex];
convertedString[numberIndex] = convertedString[swpIndex] -
convertedString[numberIndex];
convertedString[swpIndex] = convertedString[swpIndex] -
convertedString[numberIndex];
swpIndex--;
numberIndex++;
}
convertedString[numberIndex] = 0;
}
char* uIntToString(uint64_t num) {
numberIndex = 0;
uIntToStrHelper(num);
return convertedString;
}
char* IntToString(long long num) {
numberIndex = 0;
if (num < 0) {
convertedString[numberIndex++] = '-';
num = -num;
}
uIntToStrHelper(num);
return convertedString;
}
编辑:添加inttoString
和uinttoString
。
(我不知道这样做的正确方法,但这足够了)
问题的概述: “ D”和“我”的案例显示了问题的位置。inttostring的功能原型
:
char* IntToString(long long num);
// part of the reason why I used long long as va_arg data type
// had also tried with long but with no luck :(
我尝试了inttostring
&amp; uintToString
在Linux计算机上功能,它们以预期的方式工作。
注意:我尚未测试kprintf(const char* stringformat,...)
在受控&amp;像上面提到的其他两个功能一样,易于辩论的环境。
doubleTostostring
和uhextoString
等其他功能尚未进行测试,但无论如何它都不应更改/与问题相关。
kprintfhelper
是一个函数,就像kprintf
,但不进行任何检查,只打印字符串
我的环境
编译器:x86_64-elf-fcc
flags(又名cflags):-ffreestanding -mno-red-Zone -m64
链接器:x86_64-fld-ld
i使用QEMU-SYSTEM-X86_64
运行最终可执行文件。
测试
我尝试了以下情况:
- 正常的未签名案例
kprintf("Number: %d\n", 1234);
输出:
编号:1234
- 不起作用,但输出似乎
va_args
正在做一些奇怪的无符号int
数学(通过测试5进一步证明,确实是va_args
,IMO)
kprintf("Number: %d\n", -1234);
输出:
编号:4294966062
- 按预期
kprintf("Number: %d\n", 1099511627775);
输出工作:
编号:1099511627775
- 但此
kprintf("Number: %d\n", -1099511627775);
输出:
编号:1099511627775
- ,此处
kprintf("Number: %s\n", IntToString(-1234));
输出:
编号:-1234
I was designing my kernel using C. While making the kprintf
function (a function like printf
but works with the kernel) I saw that signed integers (precisely the data type is long
), va_args
is converting them to unsigned long
.
Here's a snippet of the code:kPrint.c
#include <stdarg.h>
// skipped some lines not needed for this question
// ...
/******************
* global variables needed for number to string conversion
* ***************/
// store the converted string temporarily
static char convertedString[30];
// store the index of the filled portion
static char numberIndex;
// TODO: Create function like printf but which works here
_Bool kprintf(const char* stringFormat, ...) {
char* strPtr = (char*)stringFormat;
va_list arguments;
va_start(arguments, stringFormat);
while (*strPtr != 0) {
switch (*strPtr)
{
case '\e':
// printf() also ends the execution
return 1;
case '%':
switch (*(strPtr + 1))
{
// signed decimal integer
case 'd':
case 'i':
kprintf(IntToString(va_arg(arguments, long long))); // stringify the integer
// *problem* here
strPtr += 2;
break;
// unsigned decimal integer
case 'u':
kprintf(uIntToString(va_arg(arguments, uint64_t))); // stringify
strPtr += 2;
break;
// will implement U & L case later
// unsigned hexadecimal
case 'x':
case 'X':
kprintf(uHexToString(va_arg(arguments, uint64_t))); // stringify
// doesn't work now
strPtr += 2;
break;
// will implement U & L case later
// 6 numbers after decimal
case 'f':
case 'F':
kprintf(DoubleToString(va_arg(arguments, double), 6)); // stringify
// doesn't work now
strPtr += 2;
break;
// 2 numbers after pt
case 'g':
case 'G':
kprintf(DoubleToString(va_arg(arguments, double), 2));
strPtr += 2;
break;
case 'c':
kPrintChar((char)va_arg(arguments, int));
//
strPtr += 2;
break;
// another string
case 's':
kPrintfHelper(va_arg(arguments, char*)); // just to prevent recursion
strPtr += 2;
break;
case '%':
kPrintChar('%');
strPtr += 2;
break;
}
break;
default:
kPrintChar(*strPtr);
strPtr++;
}
}
va_end(arguments);
return 0;
}
void uIntToStrHelper(uint64_t num) {
if (num < 10)
{
convertedString[numberIndex++] = num + '0';
convertedString[numberIndex] = 0;
return;
}
uint8_t numIndexCpy = numberIndex;
while (num > 0) {
convertedString[numberIndex++] = (num % 10) + '0';
num /= 10;
}
char swpIndex = (numberIndex - 2 + numIndexCpy) / 2;
numberIndex = numberIndex - swpIndex - 1 + numIndexCpy;
while (swpIndex >= numIndexCpy) {
convertedString[swpIndex] = convertedString[swpIndex] +
convertedString[numberIndex];
convertedString[numberIndex] = convertedString[swpIndex] -
convertedString[numberIndex];
convertedString[swpIndex] = convertedString[swpIndex] -
convertedString[numberIndex];
swpIndex--;
numberIndex++;
}
convertedString[numberIndex] = 0;
}
char* uIntToString(uint64_t num) {
numberIndex = 0;
uIntToStrHelper(num);
return convertedString;
}
char* IntToString(long long num) {
numberIndex = 0;
if (num < 0) {
convertedString[numberIndex++] = '-';
num = -num;
}
uIntToStrHelper(num);
return convertedString;
}
EDIT: Added IntToString
and uIntToString
.
(I don't know the correct way to do this, but it suffices)
Overview of the problem:
cases for 'd' and 'i' show the place of the problem.
Function prototype of IntToString
:
char* IntToString(long long num);
// part of the reason why I used long long as va_arg data type
// had also tried with long but with no luck :(
I have tried IntToString
& uIntToString
functions on a linux machine and they work the way intended.
NOTE: I have not tested kprintf(const char* stringFormat, ...)
in a controlled & easily debuggable environment like the other 2 functions mentioned just above.
Other functions like DoubleToString
and uHexToString
have not yet been tested but it should not change/be related to the question anyway.
kPrintfHelper
is a function just like kprintf
but does not do any checking and just prints the string
My Environment
Compiler: x86_64-elf-gcc
Flags (aka CFLAGS): -ffreestanding -mno-red-zone -m64
Linker: x86_64-elf-ld
I use qemu-system-x86_64
to run the final executable.
Tests
I have tried the following cases:
- Normal unsigned case
kprintf("Number: %d\n", 1234);
Output:
Number: 1234
- Does not work but the output seems like
va_args
is doing some weird unsignedint
math (further proved by test 5 that it was indeedva_args
, IMO)
kprintf("Number: %d\n", -1234);
Output:
Number: 4294966062
- Works as intended
kprintf("Number: %d\n", 1099511627775);
Output:
Number: 1099511627775
- BUT This
kprintf("Number: %d\n", -1099511627775);
Output:
Number: 1099511627775
- AND here
kprintf("Number: %s\n", IntToString(-1234));
Output:
Number: -1234
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您要传递的
%d
指定符的值类型与va_arg
期望与之匹配。您正在告诉
va_arg
期望长
,但是1234
和-1234
有类型int
。这些类型是不同的尺寸,因此va_arg
从呼叫堆栈中拉出的字节比您所付出的字节更多。标准格式指定器具有尺寸修饰符,因此可以处理不同的尺寸类型。您需要实施类似的东西。
The type of the value you're passing in for your
%d
specifier doesn't match whatva_arg
is expecting.You're telling
va_arg
to expect along long
, but1234
and-1234
have typeint
. These types are different sizes, sova_arg
is pulling more bytes from the call stack than you put on.The standard format specifiers have size modifiers so they can handle different size types. You'll need to implement something similar.
呼叫
kprintf(“编号:%d \ n”,-1234);
是不正确的,因为%d
提取long long long
。它必须是kprintf(“编号:%d \ n”,-1234ll);
。-1234是32位操作数。问题可能是,这是用一个64位对齐的单词传递的,但没有符号扩展到64位。
也就是说,64位中的-1234值需要为
ffffffffffffffb2e
,但是32位参数正在堆栈上产生00000000000000000000FFFFFB2E
图像,该图像是429496666662。根据这一假设,我们必须通过-1000才能获得观察到的429496629。它与-1234没有关系。其他事情可能正在发生,例如被解释为数据的垃圾位。
毕竟,这种行为的定义不明确:您将一个大小的整数推入完全不典型且不安全的参数传递机构,并拔出一个不同尺寸的整数。
The call
kprintf("Number: %d\n", -1234);
is incorrect because%d
extracts along long
. It must bekprintf("Number: %d\n", -1234LL);
.-1234 is a 32 bit operand. The problem could be that this is being passed in a 64 bit aligned word, but not being sign extended to 64 bits.
So that is to say, the -1234 value in 64 bits needs to be
fffffffffffffb2e
, but the 32 bit parameter is producing a00000000fffffb2e
image on the stack, which is 4294966062.According to this hypothesis, we would have to pass -1000 to obtain the observed 429496629, however. It bears no relation to -1234. Something else could be going on, like garbage bits being interpreted as data.
The behavior is not well-defined, after all: you're shoving an integer of one size into a completely typeless and unsafe parameter passing mechanism and pulling out an integer of a different size.