Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
adam.huang
htop
Commits
27b470e1
Commit
27b470e1
authored
Aug 29, 2011
by
Hisham Muhammad
Browse files
Don't simply trust that string splits were successful...
parent
5dfb46e1
Changes
4
Hide whitespace changes
Inline
Side-by-side
ProcessList.c
View file @
27b470e1
...
@@ -475,10 +475,14 @@ static void ProcessList_readCGroupFile(Process* process, const char* dirname, co
...
@@ -475,10 +475,14 @@ static void ProcessList_readCGroupFile(Process* process, const char* dirname, co
char
*
ok
=
fgets
(
buffer
,
255
,
file
);
char
*
ok
=
fgets
(
buffer
,
255
,
file
);
if
(
ok
)
{
if
(
ok
)
{
char
*
trimmed
=
String_trim
(
buffer
);
char
*
trimmed
=
String_trim
(
buffer
);
char
**
fields
=
String_split
(
trimmed
,
':'
);
int
nFields
;
char
**
fields
=
String_split
(
trimmed
,
':'
,
&
nFields
);
free
(
trimmed
);
free
(
trimmed
);
if
(
nFields
>=
3
)
{
process
->
cgroup
=
strndup
(
fields
[
2
]
+
1
,
10
);
process
->
cgroup
=
strndup
(
fields
[
2
]
+
1
,
10
);
}
else
{
process
->
cgroup
=
strdup
(
""
);
}
String_freeArray
(
fields
);
String_freeArray
(
fields
);
}
}
fclose
(
file
);
fclose
(
file
);
...
...
Settings.c
View file @
27b470e1
...
@@ -34,10 +34,10 @@ void Settings_delete(Settings* this) {
...
@@ -34,10 +34,10 @@ void Settings_delete(Settings* this) {
static
void
Settings_readMeters
(
Settings
*
this
,
char
*
line
,
HeaderSide
side
)
{
static
void
Settings_readMeters
(
Settings
*
this
,
char
*
line
,
HeaderSide
side
)
{
char
*
trim
=
String_trim
(
line
);
char
*
trim
=
String_trim
(
line
);
char
**
ids
=
String_split
(
trim
,
' '
);
int
nIds
;
char
**
ids
=
String_split
(
trim
,
' '
,
&
nIds
);
free
(
trim
);
free
(
trim
);
int
i
;
for
(
int
i
=
0
;
ids
[
i
];
i
++
)
{
for
(
i
=
0
;
ids
[
i
]
!=
NULL
;
i
++
)
{
Header_createMeter
(
this
->
header
,
ids
[
i
],
side
);
Header_createMeter
(
this
->
header
,
ids
[
i
],
side
);
}
}
String_freeArray
(
ids
);
String_freeArray
(
ids
);
...
@@ -45,10 +45,10 @@ static void Settings_readMeters(Settings* this, char* line, HeaderSide side) {
...
@@ -45,10 +45,10 @@ static void Settings_readMeters(Settings* this, char* line, HeaderSide side) {
static
void
Settings_readMeterModes
(
Settings
*
this
,
char
*
line
,
HeaderSide
side
)
{
static
void
Settings_readMeterModes
(
Settings
*
this
,
char
*
line
,
HeaderSide
side
)
{
char
*
trim
=
String_trim
(
line
);
char
*
trim
=
String_trim
(
line
);
char
**
ids
=
String_split
(
trim
,
' '
);
int
nIds
;
char
**
ids
=
String_split
(
trim
,
' '
,
&
nIds
);
free
(
trim
);
free
(
trim
);
int
i
;
for
(
int
i
=
0
;
ids
[
i
];
i
++
)
{
for
(
i
=
0
;
ids
[
i
]
!=
NULL
;
i
++
)
{
int
mode
=
atoi
(
ids
[
i
]);
int
mode
=
atoi
(
ids
[
i
]);
Header_setMode
(
this
->
header
,
i
,
mode
,
side
);
Header_setMode
(
this
->
header
,
i
,
mode
,
side
);
}
}
...
@@ -67,13 +67,19 @@ static bool Settings_read(Settings* this, char* fileName) {
...
@@ -67,13 +67,19 @@ static bool Settings_read(Settings* this, char* fileName) {
char
buffer
[
maxLine
];
char
buffer
[
maxLine
];
bool
readMeters
=
false
;
bool
readMeters
=
false
;
while
(
fgets
(
buffer
,
maxLine
,
fd
))
{
while
(
fgets
(
buffer
,
maxLine
,
fd
))
{
char
**
option
=
String_split
(
buffer
,
'='
);
int
nOptions
;
char
**
option
=
String_split
(
buffer
,
'='
,
&
nOptions
);
if
(
nOptions
<
2
)
{
String_freeArray
(
option
);
continue
;
}
if
(
String_eq
(
option
[
0
],
"fields"
))
{
if
(
String_eq
(
option
[
0
],
"fields"
))
{
char
*
trim
=
String_trim
(
option
[
1
]);
char
*
trim
=
String_trim
(
option
[
1
]);
char
**
ids
=
String_split
(
trim
,
' '
);
int
nIds
;
char
**
ids
=
String_split
(
trim
,
' '
,
&
nIds
);
free
(
trim
);
free
(
trim
);
int
i
,
j
;
int
i
,
j
;
for
(
j
=
0
,
i
=
0
;
i
<
LAST_PROCESSFIELD
&&
ids
[
i
]
!=
NULL
;
i
++
)
{
for
(
j
=
0
,
i
=
0
;
i
<
LAST_PROCESSFIELD
&&
ids
[
i
];
i
++
)
{
// This "+1" is for compatibility with the older enum format.
// This "+1" is for compatibility with the older enum format.
int
id
=
atoi
(
ids
[
i
])
+
1
;
int
id
=
atoi
(
ids
[
i
])
+
1
;
if
(
id
>
0
&&
id
<
LAST_PROCESSFIELD
)
{
if
(
id
>
0
&&
id
<
LAST_PROCESSFIELD
)
{
...
...
String.c
View file @
27b470e1
...
@@ -55,7 +55,8 @@ inline int String_eq(const char* s1, const char* s2) {
...
@@ -55,7 +55,8 @@ inline int String_eq(const char* s1, const char* s2) {
return
(
strcmp
(
s1
,
s2
)
==
0
);
return
(
strcmp
(
s1
,
s2
)
==
0
);
}
}
char
**
String_split
(
const
char
*
s
,
char
sep
)
{
char
**
String_split
(
const
char
*
s
,
char
sep
,
int
*
n
)
{
*
n
=
0
;
const
int
rate
=
10
;
const
int
rate
=
10
;
char
**
out
=
(
char
**
)
malloc
(
sizeof
(
char
*
)
*
rate
);
char
**
out
=
(
char
**
)
malloc
(
sizeof
(
char
*
)
*
rate
);
int
ctr
=
0
;
int
ctr
=
0
;
...
@@ -83,6 +84,7 @@ char** String_split(const char* s, char sep) {
...
@@ -83,6 +84,7 @@ char** String_split(const char* s, char sep) {
}
}
out
=
realloc
(
out
,
sizeof
(
char
*
)
*
(
ctr
+
1
));
out
=
realloc
(
out
,
sizeof
(
char
*
)
*
(
ctr
+
1
));
out
[
ctr
]
=
NULL
;
out
[
ctr
]
=
NULL
;
*
n
=
ctr
;
return
out
;
return
out
;
}
}
...
...
String.h
View file @
27b470e1
...
@@ -27,7 +27,7 @@ char* String_trim(const char* in);
...
@@ -27,7 +27,7 @@ char* String_trim(const char* in);
extern
int
String_eq
(
const
char
*
s1
,
const
char
*
s2
);
extern
int
String_eq
(
const
char
*
s1
,
const
char
*
s2
);
char
**
String_split
(
const
char
*
s
,
char
sep
);
char
**
String_split
(
const
char
*
s
,
char
sep
,
int
*
n
);
void
String_freeArray
(
char
**
s
);
void
String_freeArray
(
char
**
s
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment