K&R 中的 getop() 实现容易受到缓冲区溢出的影响吗?

问题描述 投票:0回答:0

getop()-pag的代码。来自 K&R 2ed 的 78 - 如下:

int getch(void);
void ungetch(int);

/* getop: get next character or numeric operand */
int getop(char s[])
{
    int i, c;
    while ((s[0] = c = getch()) == ' ' || c == '\t')
        ;
    s[1] = '\0';

    if (!isdigit(c) && c != '.')
        return c; /* not a number */

    i = 0;
    if (isdigit(c)) /* collect integer part */
        while (isdigit(s[++i] = c = getch()))
            ;
    if (c == '.') /* collect fraction part */
        while (isdigit(s[++i] = c = getch()))
            ;
    s[i] = '\0';

    if (c != EOF)
        ungetch(c);

    return NUMBER;
}

getch
只是
getchar
的包装,我们可以认为它是一样的)

那个函数不是容易受到缓冲区溢出的影响吗?例如,当用户输入一个 20 位长整数且

s
大小仅为 10 时,不会进行任何大小检查。

但是,我无法使程序崩溃,即使我将

s
定义为大小 2 并且输入是非常非常长的 1111111...1111111,它只是正确地给我
inf
.

c buffer-overflow
© www.soinside.com 2019 - 2024. All rights reserved.