[Asterisk-Dev] fun with valgrind
Dan Evans
devans at invores.com
Tue May 10 09:44:58 MST 2005
The interesting point here is that the C string library functions, with
the exception of memmove, have undefined behavior for overlapping source
and destination. And this design potentially moves some characters
several times; a different approach would move each character once or
not at all.
Dan
Gilad Ben-Yossef wrote:
> Matthew Boehm wrote:
>
>> /* substitute strings \t and \n into the apropriate characters */
>> tmpread = tmpwrite = emailbody;
>> while ((tmpwrite = strchr(tmpread,'\\'))) {
>> int len = strlen("\n");
>> switch (tmpwrite[1]) {
>> case 'n':
>> strncpy(tmpwrite+len,tmpwrite+2,strlen(tmpwrite+2)+1);
>> <=== line
>> 5545
>> strncpy(tmpwrite,"\n",len);
>> break;
>> case 't':
>> strncpy(tmpwrite+len,tmpwrite+2,strlen(tmpwrite+2)+1);
>> <=== line
>> 5549
>> strncpy(tmpwrite,"\t",len);
>> break;
>> default:
>> ast_log(LOG_NOTICE, "Substitution routine does not support
>> this
>> character: %c\n",tmpwrite[1]);
>> }
>> tmpread = tmpwrite+len;
>> }
>>
>> Could somebody explain what is going on there? It seems as if the code is
>> replacing "\n" with "\n" and "\t" with "\t".
>>
>
> That is exactly what it is doing - except that it replaces the literal
> characters '\' and 't', for example, with the ASCII code for tab, which
> in C is written '\t'.
>
> In other words, the code in Asterisk that replcaes the characters "\t"
> with the ASCII value for a tab works thanks to the fact that the C
> compiler does exactly the same thing in a kind of recursive way that
> reminds me of Scheme programs... :-)
>
> Gilad
>
More information about the asterisk-dev
mailing list