[PATCH] perf jvmti: Properly handle return value checks in jvmti_write_code
From: Luo Yifan
Date: Tue Nov 12 2024 - 01:59:10 EST
Following the approach in the jvmti_write_debug_info function, add
some return value checks in jvmti_write_code.
Signed-off-by: Luo Yifan <luoyifan@xxxxxxxxxxxxxxxxxxxx>
---
tools/perf/jvmti/jvmti_agent.c | 27 +++++++++++++++++----------
1 file changed, 17 insertions(+), 10 deletions(-)
diff --git a/tools/perf/jvmti/jvmti_agent.c b/tools/perf/jvmti/jvmti_agent.c
index 526dcaf9f..b52466a0c 100644
--- a/tools/perf/jvmti/jvmti_agent.c
+++ b/tools/perf/jvmti/jvmti_agent.c
@@ -361,9 +361,8 @@ jvmti_write_code(void *agent, char const *sym,
{
static int code_generation = 1;
struct jr_code_load rec;
- size_t sym_len;
+ size_t sret, sym_len;
FILE *fp = agent;
- int ret = -1;
/* don't care about 0 length function, no samples */
if (size == 0)
@@ -400,17 +399,25 @@ jvmti_write_code(void *agent, char const *sym,
*/
rec.code_index = code_generation++;
- ret = fwrite_unlocked(&rec, sizeof(rec), 1, fp);
- fwrite_unlocked(sym, sym_len, 1, fp);
-
- if (code)
- fwrite_unlocked(code, size, 1, fp);
+ sret = fwrite_unlocked(&rec, sizeof(rec), 1, fp);
+ if (sret != 1)
+ goto error;
- funlockfile(fp);
+ sret = fwrite_unlocked(sym, sym_len, 1, fp);
+ if (sret != 1)
+ goto error;
- ret = 0;
+ if (code) {
+ sret = fwrite_unlocked(code, size, 1, fp);
+ if (sret != 1)
+ goto error;
+ }
- return ret;
+ funlockfile(fp);
+ return 0;
+error:
+ funlockfile(fp);
+ return -1;
}
int
--
2.33.0