Ignore:
Timestamp:
Feb 7, 2016, 7:47:38 PM (8 years ago)
Author:
alain
Message:

Fixing two bugs:
1) In the string library: the strcpy() function must copy
the terminating NUL character in the destination buffer.
2) In the malloc library: the free() function must reset
the entry associated to the released buffer in the alloc[array].

File:
1 edited

Legend:

Unmodified
Added
Removed
  • soft/giet_vm/giet_libs/string.c

    r777 r781  
    66///////////////////////////////////////////////////////////////////////////////////
    77
    8 ///////////////////////////////////////////////////////////////////////////////////
    9 // char * strcpy (char * destination, const char * source)
    10 ///////////////////////////////////////////////////////////////////////////////////
     8///////////////////////////////////////////////////////
    119char * strcpy (char * destination, const char * source)
    1210{
    13     if (!destination || !source) {
    14         return destination;
    15     }
     11    if (!destination || !source) return destination;
    1612
    17     while (*source) {
    18         *(destination++) = *(source++);
    19     }
     13    char* src = source;
     14    char* dst = destination;
     15    do
     16    {
     17        *dst = *src;
     18        dst++;
     19        src++;
     20    }  while (*src);
     21
     22    *dst = 0;
    2023
    2124    return destination;
     
    2326
    2427
    25 ///////////////////////////////////////////////////////////////////////////////////
    26 // char * strncpy (char * destination, const char * source, int maxlen)
    27 ///////////////////////////////////////////////////////////////////////////////////
     28////////////////////////////////////////////////////
    2829char * strncpy(char * dest, const char * src, int n)
    2930{
    3031    int i;
    31     for (i = 0; i < n && src[i] != '\0'; i++) {
     32    for (i = 0; i < n && src[i] != '\0'; i++)
     33    {
    3234        dest[i] = src[i];
    3335    }
    34     for (; i < n ; i++) {
     36    for (; i < n ; i++)
     37    {
    3538        dest[i] = '\0';
    3639    }
     40
    3741    return dest;
    3842}
    3943
    4044
    41 ///////////////////////////////////////////////////////////////////////////////////
    42 // int strcmp (const char * str1, const char * str2)
    43 ///////////////////////////////////////////////////////////////////////////////////
     45/////////////////////////////////////////////////
    4446int strcmp (const char * str1, const char * str2)
    4547{
    46     if (!str1 || !str2) {
    47         return -123456; // return a value out of the char's bounds
     48    if (!str1 || !str2)
     49    {
     50        return -123456;     // return a value out of the char's bounds
    4851    }
    4952
    50     while (*str1 && *str1 == *str2) {
     53    while (*str1 && *str1 == *str2)
     54    {
    5155        str1++;
    5256        str2++;
     
    5660}
    5761
    58 ///////////////////////////////////////////////////////////////////////////////////
    59 // int strlen ( const char * str )
    60 ///////////////////////////////////////////////////////////////////////////////////
     62/////////////////////////////
    6163int strlen (const char * str)
    6264{
    6365    const char * s = str;
    6466
    65     while (*s) {
     67    while (*s)
     68    {
    6669        s++;
    6770    }
     
    7073
    7174
    72 ///////////////////////////////////////////////////////////////////////////////////
    73 // char * strchr(const char * str)
    74 ///////////////////////////////////////////////////////////////////////////////////
     75//////////////////////////////////////
    7576char * strchr(const char * str, int c)
    7677{
    7778    const char * s = str;
    7879    const char ch = c;
    79     while (*s != ch && *s != '\0') {
     80    while (*s != ch && *s != '\0')
     81    {
    8082        s++;
    8183    }
Note: See TracChangeset for help on using the changeset viewer.