Changeset 1586

Show
Ignore:
Timestamp:
03/10/10 16:31:06 (5 months ago)
Author:
marek
Message:

batctl: fix crash in bat-hosts parser on embedded systems

The bat-hosts parser uses the realpath() function to recognize already
parsed files. On most systems realpath() allocates a buffer containing
the file's location. Some embedded libc implementations seem to expect
an allocated buffer as second argument otherwise they simply crash.
batctl now always allocates a buffer to handle this case.

Reported-by: Linus Luessing <linus.luessing@…>
Signed-off-by: Marek Lindner <lindner_marek@…>

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/batctl/bat-hosts.c

    r1585 r1586  
    151151        char *homedir; 
    152152        size_t locations = sizeof(bat_hosts_path) / sizeof(char *); 
    153         char *normalized[locations]; 
    154  
     153        char *normalized; 
     154 
     155        /*** 
     156         * realpath could allocate the memory for us but some embedded libc 
     157         * implementations seem to expect a buffer as second argument 
     158         */ 
     159        normalized = malloc(locations * PATH_MAX); 
     160        if (!normalized) { 
     161                printf("Warning - couldn't not get memory for bat-hosts file parsing\n"); 
     162                return; 
     163        } 
     164 
     165        memset(normalized, 0, locations * PATH_MAX); 
    155166        host_hash = hash_new(64, compare_mac, choose_mac); 
    156167 
     
    175186                } 
    176187 
    177                 normalized[i] = realpath(confdir, NULL); 
    178                 if (normalized[i] == NULL) 
     188                if (!realpath(confdir, normalized + (i * PATH_MAX))) 
    179189                        continue; 
    180190 
     
    182192                parse = 1; 
    183193                for (j = 0; j < i; j++) { 
    184                         if (normalized[j] == NULL) 
    185                                 continue; 
    186  
    187                         if (strncmp(normalized[i], normalized[j], CONF_DIR_LEN) == 0) { 
     194                        if (strncmp(normalized + (i * PATH_MAX), normalized + (j * PATH_MAX), CONF_DIR_LEN) == 0) { 
    188195                                parse = 0; 
    189196                                break; 
     
    191198                } 
    192199 
    193                 if (parse && (normalized[i] != NULL)) 
    194                         parse_hosts_file(&host_hash, normalized[i]); 
    195         } 
    196  
    197         for (i = 0; i < locations; i++) { 
    198                 if (normalized[i]) 
    199                         free(normalized[i]); 
    200         } 
     200                if (parse) 
     201                        parse_hosts_file(&host_hash, normalized + (i * PATH_MAX)); 
     202        } 
     203 
     204        free(normalized); 
    201205} 
    202206