Make get_my_path() safer

Adds maxLen parameter to get_my_path().
Some small cosmetic fixes.
This commit is contained in:
Alexey Tarasov 2009-10-22 02:55:00 +11:00
parent 74d7ff8cfd
commit 3166410a82
6 changed files with 20 additions and 17 deletions

View File

@ -783,7 +783,7 @@ int launch_server()
fprintf(stderr, "pipe failed in launch_server, errno: %d\n", errno);
return -1;
}
get_my_path(path);
get_my_path(path, PATH_MAX);
pid_t pid = fork();
if(pid < 0) return -1;

View File

@ -236,7 +236,7 @@ void fatal_errno(const char *fmt, ...);
void handle_packet(apacket *p, atransport *t);
void send_packet(apacket *p, atransport *t);
void get_my_path(char s[PATH_MAX]);
void get_my_path(char *s, size_t maxLen);
int launch_server();
int adb_main(int is_daemon);

View File

@ -50,7 +50,7 @@ enum {
static int do_cmd(transport_type ttype, char* serial, char *cmd, ...);
void get_my_path(char s[PATH_MAX]);
void get_my_path(char *s, size_t maxLen);
int find_sync_dirs(const char *srcarg,
char **android_srcdir_out, char **data_srcdir_out);
int install_app(transport_type transport, char* serial, int argc, char** argv);
@ -673,7 +673,7 @@ static char *find_top(char path_buf[PATH_MAX])
/* If the CWD isn't under a good-looking top, see if the
* executable is.
*/
get_my_path(dir);
get_my_path(dir, PATH_MAX);
top = find_top_from(dir, path_buf);
}
return top;

View File

@ -17,7 +17,7 @@
#import <Carbon/Carbon.h>
#include <unistd.h>
void get_my_path(char s[PATH_MAX])
void get_my_path(char *s, size_t maxLen)
{
ProcessSerialNumber psn;
GetCurrentProcess(&psn);
@ -25,6 +25,6 @@ void get_my_path(char s[PATH_MAX])
dict = ProcessInformationCopyDictionary(&psn, 0xffffffff);
CFStringRef value = (CFStringRef)CFDictionaryGetValue(dict,
CFSTR("CFBundleExecutable"));
CFStringGetCString(value, s, PATH_MAX - 1, kCFStringEncodingUTF8);
CFStringGetCString(value, s, maxLen, kCFStringEncodingUTF8);
}

View File

@ -19,15 +19,15 @@
#include <limits.h>
#include <stdio.h>
void get_my_path(char exe[PATH_MAX])
void get_my_path(char *exe, size_t maxLen)
{
char proc[64];
snprintf(proc, sizeof proc, "/proc/%d/exe", getpid());
int err = readlink(proc, exe, PATH_MAX - 1);
int err = readlink(proc, exe, maxLen - 1);
if(err > 0) {
exe[err] = 0;
exe[err] = '\0';
} else {
exe[0] = 0;
exe[0] = '\0';
}
}

View File

@ -18,14 +18,17 @@
#include <assert.h>
#include <windows.h>
void get_my_path(char exe[PATH_MAX])
void get_my_path(char *exe, size_t maxLen)
{
char* r;
char *r;
GetModuleFileName( NULL, exe, PATH_MAX-1 );
exe[PATH_MAX-1] = 0;
r = strrchr( exe, '\\' );
if (r)
*r = 0;
/* XXX: should be GetModuleFileNameA */
if (GetModuleFileName(NULL, exe, maxLen) > 0) {
r = strrchr(exe, '\\');
if (r != NULL)
*r = '\0';
} else {
exe[0] = '\0';
}
}