Author: marek Date: 2010-03-10 23:34:43 +0100 (Wed, 10 Mar 2010) New Revision: 1591
Modified: branches/batctl-0.2.x/bat-hosts.c Log: 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@web.de Signed-off-by: Marek Lindner lindner_marek@yahoo.de
Modified: branches/batctl-0.2.x/bat-hosts.c =================================================================== --- branches/batctl-0.2.x/bat-hosts.c 2010-03-10 22:34:41 UTC (rev 1590) +++ branches/batctl-0.2.x/bat-hosts.c 2010-03-10 22:34:43 UTC (rev 1591) @@ -150,8 +150,19 @@ char confdir[CONF_DIR_LEN]; char *homedir; size_t locations = sizeof(bat_hosts_path) / sizeof(char *); - char *normalized[locations]; + char *normalized;
+ /*** + * realpath could allocate the memory for us but some embedded libc + * implementations seem to expect a buffer as second argument + */ + normalized = malloc(locations * PATH_MAX); + if (!normalized) { + printf("Warning - couldn't not get memory for bat-hosts file parsing\n"); + return; + } + + memset(normalized, 0, locations * PATH_MAX); host_hash = hash_new(64, compare_mac, choose_mac);
if (!host_hash) { @@ -174,30 +185,23 @@ confdir[CONF_DIR_LEN - 1] = '\0'; }
- normalized[i] = realpath(confdir, NULL); - if (normalized[i] == NULL) + if (!realpath(confdir, normalized + (i * PATH_MAX))) continue;
/* check for duplicates: don't parse the same file twice */ parse = 1; for (j = 0; j < i; j++) { - if (normalized[j] == NULL) - continue; - - if (strncmp(normalized[i], normalized[j], CONF_DIR_LEN) == 0) { + if (strncmp(normalized + (i * PATH_MAX), normalized + (j * PATH_MAX), CONF_DIR_LEN) == 0) { parse = 0; break; } }
- if (parse && (normalized[i] != NULL)) - parse_hosts_file(&host_hash, normalized[i]); + if (parse) + parse_hosts_file(&host_hash, normalized + (i * PATH_MAX)); }
- for (i = 0; i < locations; i++) { - if (normalized[i]) - free(normalized[i]); - } + free(normalized); }
struct bat_host *bat_hosts_find_by_name(char *name)