Browse Source

Do not use fixed size PATH_MAX to allocate memory in functions like pstat_get_binary_path_from...

master
Aitor 3 months ago
parent
commit
80899abd65
  1. 78
      libpstat.c
  2. 5
      libpstat.h

78
libpstat.c

@ -90,7 +90,7 @@ uint64_t pstat_get_starttime( struct pstat* ps ) {
}
// Returned array must be freed to prevent memory leaks
char * pstat_get_binary_path_from_string(char *buffer, const char *pid_value)
int pstat_get_binary_path_from_string(char **buf, int sz, const char *pid_value)
{
pid_t pid = 0;
int rc = 0;
@ -98,9 +98,8 @@ char * pstat_get_binary_path_from_string(char *buffer, const char *pid_value)
struct stat bin_stat;
struct pstat* ps = pstat_new();
if( ps == NULL ) {
if( ps == NULL )
exit(EXIT_FAILURE);
}
rc = sscanf( pid_value, "%d", &pid );
if( rc != 1 ) {
@ -115,28 +114,31 @@ char * pstat_get_binary_path_from_string(char *buffer, const char *pid_value)
exit(EXIT_FAILURE);
}
if( !pstat_is_running( ps ) ) {
return buffer;
}
if( !pstat_is_running(ps) )
return 1;
if( !pstat_is_deleted( ps ) ) {
if( !pstat_is_deleted(ps) ) {
buffer = (char*)malloc((PATH_MAX+1) * sizeof(char));
if(buffer == NULL) {
*buf = (char*)malloc(sz * sizeof(char));
if(*buf == NULL) {
fprintf( stderr, "Memory allocation failure: %d\n", rc );
exit(EXIT_FAILURE);
}
if((int)strlen(ps->path) > sz) {
pstat_free(ps);
return 1;
}
pstat_get_stat( ps, &bin_stat );
pstat_get_path( ps, buffer );
strcpy(*buf, ps->path);
}
pstat_free( ps );
return buffer;
return 0;
}
char * pstat_get_binary_path_from_integer(char *buffer, pid_t pid)
int pstat_get_binary_path_from_integer(char **buf, int sz, pid_t pid)
{
int rc = 0;
@ -153,25 +155,28 @@ char * pstat_get_binary_path_from_integer(char *buffer, pid_t pid)
exit(EXIT_FAILURE);
}
if( !pstat_is_running( ps ) ) {
return buffer;
}
if( !pstat_is_running(ps) )
return 1;
if( !pstat_is_deleted( ps ) ) {
if( !pstat_is_deleted(ps) ) {
buffer = (char*)malloc((PATH_MAX+1) * sizeof(char));
if(buffer == NULL) {
*buf = (char*)malloc(sz * sizeof(char));
if(*buf == NULL) {
fprintf( stderr, "Memory allocation failure: %d\n", rc );
exit(EXIT_FAILURE);
}
pstat_get_stat( ps, &bin_stat );
pstat_get_path( ps, buffer );
if((int)strlen(ps->path) > sz) {
pstat_free(ps);
return 1;
}
pstat_get_stat(ps, &bin_stat);
strcpy(*buf, ps->path);
}
pstat_free( ps );
return buffer;
return 0;
}
uint64_t pstat_starttime_from_integer(pid_t pid)
@ -199,34 +204,3 @@ uint64_t pstat_starttime_from_integer(pid_t pid)
return start_time;
}
dev_t pstat_get_dev_from_integer(pid_t pid)
{
int rc = 0;
dev_t dev = 0;
struct stat bin_stat;
struct pstat* ps = pstat_new();
if( ps == NULL ) {
exit(EXIT_FAILURE);
}
rc = pstat( pid, ps, 0 );
if( rc != 0 ) {
fprintf( stderr, "pstat(%d) rc = %d\n", pid, rc );
exit(EXIT_FAILURE);
}
if( !pstat_is_running( ps ) || pstat_is_deleted( ps ) ) {
return 0;
}
pstat_get_stat( ps, &bin_stat );
dev = bin_stat.st_dev;
pstat_free( ps );
return dev;
}

5
libpstat.h

@ -80,10 +80,9 @@ int pstat_get_stat( struct pstat* ps, struct stat* sb );
uint64_t pstat_get_starttime( struct pstat* ps );
// Utilities for external use
char* pstat_get_binary_path_from_string(char*, const char*);
char* pstat_get_binary_path_from_integer(char*, pid_t);
int pstat_get_binary_path_from_string(char**, int, const char*);
int pstat_get_binary_path_from_integer(char**, int, pid_t);
uint64_t pstat_starttime_from_integer(pid_t);
dev_t pstat_get_dev_from_integer(pid_t pid);
#ifdef __cplusplus

Loading…
Cancel
Save