如何在不丢失任何数据的情况下在函数之间传递变量

huangapple go评论100阅读模式
英文:

How to pass a variable between function without loose any data

问题

I have a big problem, i have build a code where I catch rtc data and pass it to a string , with this I made the name of the file, based in year/month/day/hour/minuts/seconds. This way I assure there is no way to have two files with same name.

Based in this spintf convertion I send it to another page where I try to creat the file , but something goes wrong because this string does not arrive full.
example:

file1.c

char dateAndTimeFile[];
void setUserDefinedTime(int rtc_year, int rtc_month, int rtc_day, int rtc_hour, int rtc_minute, int rtc_second){
    /*CONVERT INTO SINGLE EXPRESSION FOR TIME FILE*/
     sprintf(dateAndTimeFile, "HealthFile20%02d%02d%02d%02d%02d%02dLOG.txt",
     rtc_year, rtc_month, rtc_day, rtc_hour, rtc_minute, rtc_second);
     setParameterCreateInputFile(dateAndTimeFile);
}

file2.c

char *inputfile;
void setParameterCreateInputFile(char str[]){
    inputfile = str;
    printf("file:%s\n", (TCHAR*)inputfile);

    // all FULL string present
    SDCARD_Open();
}

void SDCARD_Open(void){
    printf("LOAD %s\n", (TCHAR*)inputfile);
    //this code appear to be broken only show LOAD Heal
}
英文:

I have a big problem, i have build a code where I catch rtc data and pass it to a string , with this I made the name of the file, based in year/month/day/hour/minuts/seconds. This way I assure there is no way to have two files with same name.
Based in this spintf convertion I send it to another page where I try to creat the file , but something goes wrong because this string does not arrive full.
example:

file1.c

char dateAndTimeFile[];
void setUserDefinedTime(int rtc_year, int rtc_month, int rtc_day, int rtc_hour, int rtc_minute, int rtc_second){
    /*CONVERT INTO SINGLE EXPRESSION FOR TIME FILE*/
     sprintf(dateAndTimeFile,"HealthFile20%02d%02d%02d%02d%02d%02dLOG.txt",
     rtc_year, rtc_month, rtc_day, rtc_hour, rtc_minute, rtc_second);
     setParameterCreateInputFile(dateAndTimeFile);
}

file2.c


char *inputfile;
void setParameterCreateInputFile(char str[]){
    inputfile = str;
    printf("file:%s\n", (TCHAR*)inputfile);

// all FULL string present
SDCARD_Open();
}

void SDCARD_Open(void){
printf("LOAD %s\n", (TCHAR*)inputfile);
//this code appear to be broken only show LOAD Heal
}

答案1

得分: 2

你必须确保数组dateAndTimeFile足够大,以存储由sprintf写入的字符串。

在这行

char dateAndTimeFile[];

你没有指定数组的大小,因此无法保证大小足够。

sprintf写入的字符串长度至少为31个字符(包括空字符则为32)。根据整数参数的值,长度可能更大。

因此,你必须将数组的大小至少设为32字节,如下所示:

char dateAndTimeFile[32];

然而,除非你在内存非常有限的平台上,最好慷慨一些,例如将数组大小设置为100或200字节。否则,你将面临缓冲区溢出的风险。

为了额外的安全性,我建议你使用函数snprintf来替代sprintf。这样,即使字符串最终比预期的要长得多,也不会发生缓冲区溢出。

所以,不要使用以下方式:

sprintf(dateAndTimeFile,"HealthFile20%02d%02d%02d%02d%02d%02dLOG.txt",
     rtc_year, rtc_month, rtc_day, rtc_hour, rtc_minute, rtc_second);

我建议你改为:

int wants_to_write = snprintf(
    dateAndTimeFile, sizeof dateAndTimeFile,
    "HealthFile20%02d%02d%02d%02d%02d%02dLOG.txt",
    rtc_year, rtc_month, rtc_day,
    rtc_hour, rtc_minute, rtc_second
);
if (wants_to_write < 0 || wants_to_write >= sizeof dateAndTimeFile)
{
    fprintf(stderr, "写入缓冲区错误!\n");
    exit(EXIT_FAILURE);
}

这样,如果缓冲区不够大,你的程序将打印错误消息,而不是溢出缓冲区(可能导致程序崩溃)。

英文:

You must make the array dateAndTimeFile large enough to store the string written by sprintf.

In the line

char dateAndTimeFile[];

you are not specifying the size of the array, so it is not guaranteed that the size is sufficient.

The string that sprintf writes will have a length of at least 31 characters (32 including the null terminating character). Depending on the value of the integer arguments, the length may be larger.

Therefore, you must make the size of the array at least 32 bytes, like this:

char dateAndTimeFile[32];

However, unless you are on a platform with very little memory, it would probably be better to be generous, for example to set the size of the array to 100 or 200 bytes. Otherwise, you will risk a buffer overflow.

For additional safety, I suggest that you use the function snprintf instead of sprintf. That way, a buffer overflow cannot occur, even in cases in which the string ends up being significantly longer than intended.

Instead of

sprintf(dateAndTimeFile,&quot;HealthFile20%02d%02d%02d%02d%02d%02dLOG.txt&quot;,
     rtc_year, rtc_month, rtc_day, rtc_hour, rtc_minute, rtc_second);

I suggest that you write:

int wants_to_write = snprintf(
    dateAndTimeFile, sizeof dateAndTimeFile,
    &quot;HealthFile20%02d%02d%02d%02d%02d%02dLOG.txt&quot;,
    rtc_year, rtc_month, rtc_day,
    rtc_hour, rtc_minute, rtc_second
);
if ( wants_to_write &lt; 0 || wants_to_write &gt;= sizeof dateAndTimeFile )
{
    fprintf( stderr, &quot;Error writing to buffer!\n&quot; );
    exit( EXIT_FAILURE );
}

That way, your program will print an error message if the buffer is not large enough, instead of overflowing the buffer (which may cause the program to crash).

huangapple
  • 本文由 发表于 2023年3月7日 03:29:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75655048.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定