English 中文(简体)
我如何在没有缓冲溢流关切的情况下利用飞机?
原标题:How can I use strncat without buffer overflow concerns?

我有一个缓冲地带,我做了很多事情。 我想确保我永远不会超越缓冲规模。

char buff[64];

strcpy(buff, "String 1");

strncat(buff, "String 2", sizeof(buff));

strncat(buff, "String 3", sizeof(buff));

我不想说一小.,而是想说什么。 我想确保我永远不会凌驾于缓冲之上。

最佳回答

考虑现有方位和无效方的大小

#define BUFFER_SIZE 64
char buff[BUFFER_SIZE];

//Use strncpy
strncpy(buff, "String 1", BUFFER_SIZE - 1);
buff[BUFFER_SIZE - 1] =   ;

strncat(buff, "String 2", BUFFER_SIZE - strlen(buff) - 1);

strncat(buff, "String 3", BUFFER_SIZE - strlen(buff) - 1);
问题回答

为什么不使用<代码>snprintf? 与<代码>strncat不同的是,它期望缓冲区的规模,但更重要的是,没有隐藏的O(n)。

强硬需要寻找每一座车上的无效者,每一次都要通过整个缓冲站找到最终结果。 每当扼杀时间越长,str就放慢了。 另一方面,印本可以跟踪最终情况。 页: 1

snprintf(buf, sizeof buf, "%s%s%s", "String1", "String2", "String3");

常常是一个更快和更可读的州。

您使用<代码>strncat在贵国或外国代码中的功能,实际上适合另一功能:strlcat(脚注,而不是n)。 <代码>strlcat功能并非标准功能,而是供大众使用的<代码>strncat替换功能。 <代码>strlcat预期整个目的地缓冲总尺寸是其最后论点。

Meanwhile, strncat expects the size of the remaining unused portion of the target buffer as its third argument. For this reason, your original code is incorrect.

I would suggest that instead of doing that horrible abuse of strncpy and making explicit rescans with those strlen calls (both issues present in Joe s answer), you either use an implementation-provided strlcat or implement one yourself (if your implementation provides no strlcat).

http://en.wikipedia.org/wiki/Strlcpy

这是这样做的最佳方式。 <>代码/代码>,如果你不在当地分配数据,就只给你数据点的大小(在这种情况下,你确实在当地分配数据,但最好以这种方式这样做,如果重新计算该代码,它将发挥作用)。

#define MAXBUFFSIZE 64

char buff[MAXBUFFSIZE];

buff[0] = 0;  // or some string

strncat(buff, "String x",MAXBUFFSIZE - strlen(buff) - 1);

Hogan已经回答了问题;然而,如果你担心在<条形码>中出现缓冲溢流,那么你也应同样担心在所有其他扼杀职能中的缓冲溢流。

使用<代码>strnlen(......)和strncpy(......),以确保你确实留在你的缓冲之内。 如果没有<代码>strnlen(......)功能,请予以书写。

此处使用<代码>memccpy,而不是strncat-使之更安全、更快。 (《<>snprintf上述::

/**
 * Returns the number of bytes copied (not including terminating   ).
 * Always terminates @buf with   .
 */ 
int add_strings(char *buf, int len)
{
    char *p = buf;

    if (len <= 0)
        return 0;

    p[len - 1] =   ; /* always terminate */

    p = memccpy(buf, "String 1",   , len - 1);
    if (p == NULL)
        return len - 1;

    p = memccpy(p - 1, "String 2",   , len - 1 - (p - buf));
    if (p == NULL)
        return len - 1;

    p = memccpy(p - 1, "String 3",   , len - 1 - (p - buf));

    return (p == NULL ? len : p - buf) - 1;
}




相关问题
Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

String initialization with pair of iterators

I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...

break a string in parts

I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签