Вопрос взят из си-шного теста в августе .
Имеется функция , в которой есть ошибки .
Сделайте выбор :
char *trim (char *s) {
char *start = s;
char *fnws, *nws, *ws;
assert(start);
while (isspace(*s)) s++;
fnws = nws = s;
ws = start - 1;
while (*s) {
while (*s && !isspace(*s)) s++;
ws = s;
while (isspace(*s)) s++;
if (*s) nws = s;
}
if (ws > nws) *ws = 0;
memmove(start, fnws, strlen(fnws) + 1);
return start;
}
Варианты отыетов :
1 Если *s - нулевой указатель , это могет привести к проблемам с памятью
2 Поскольку start и fnws указывают на одно и то же , функция memmove(
не пройдет , надо использовать memcpy()
3 Если аргумент функции trim лежит в области read only , возможны проблемы
4 Если аргумент s включает токо пробелы , возможны проблемы
5 If there is trailing whitespace, a call to trim() will cause memory beyond the end
of the string to be accessed.
C/C++ : BB - test
-
- Новичок
- Posts: 86
- Joined: 27 Feb 2001 10:01
- Location: Omsk , Russia
-
- Уже с Приветом
- Posts: 3179
- Joined: 12 Jun 2001 09:01
- Location: SPb,Russia->Rehovot, Israel->Cambridge, MA
C/C++ : BB - test
Yet another proof of tests uselessness...
Who ever remembers what isspace(0) is? There is a manual for such stuff...
Well, for my platform (Solaris + Forte C) isspace(0) == 0, but I have no idea what about other platforms/compilers/versions of C-runtime library...
Who ever remembers what isspace(0) is? There is a manual for such stuff...
Well, for my platform (Solaris + Forte C) isspace(0) == 0, but I have no idea what about other platforms/compilers/versions of C-runtime library...
-
- Уже с Приветом
- Posts: 3127
- Joined: 10 Apr 2001 09:01
- Location: MD
C/C++ : BB - test
<blockquote><font size="1" face="Arial, Verdana, Helvetica, sans-serif">quote:</font><hr>Originally posted by Azazello:
<strong>Yet another proof of tests uselessness...
Who ever remembers what isspace(0) is? </strong><hr></blockquote>
0 is not a white space in POSIX
<strong>Yet another proof of tests uselessness...
Who ever remembers what isspace(0) is? </strong><hr></blockquote>
0 is not a white space in POSIX
-
- Уже с Приветом
- Posts: 3179
- Joined: 12 Jun 2001 09:01
- Location: SPb,Russia->Rehovot, Israel->Cambridge, MA
C/C++ : BB - test
<blockquote><font size="1" face="Arial, Verdana, Helvetica, sans-serif">quote:</font><hr>Originally posted by AK70:
<strong>
0 is not a white space in POSIX</strong><hr></blockquote>
Ok, why am I supposed to remember it?
There is always a manual for such stuff...
<strong>
0 is not a white space in POSIX</strong><hr></blockquote>
Ok, why am I supposed to remember it?
There is always a manual for such stuff...