From 839abfd1ce32f84321cf06b77783456fd5dad655 Mon Sep 17 00:00:00 2001 From: shenglian zhou Date: Thu, 30 Dec 2021 09:43:27 +0800 Subject: [PATCH] (query,insert,other,tools,connector,taosAdapter):fix coredump when sql to log slow query exceeds SQL_MAX_LENGTH --- src/plugins/monitor/src/monMain.c | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/plugins/monitor/src/monMain.c b/src/plugins/monitor/src/monMain.c index a03bc09036..8a7c257cc9 100644 --- a/src/plugins/monitor/src/monMain.c +++ b/src/plugins/monitor/src/monMain.c @@ -1239,41 +1239,48 @@ static void monSaveSlowQueryInfo() { monError("failed to save slow_query info, reason: invalid row %s len, sql:%s", (char *)row[i], tsMonitor.sql); goto DONE; } - pos += snprintf(sql + pos, strlen(SQL_STR_FMT) + charLen + 1, ", "SQL_STR_FMT, (char *)row[i]); + if (pos < SQL_LENGTH) + pos += snprintf(sql + pos, SQL_LENGTH - pos, ", "SQL_STR_FMT, (char *)row[i]); } else if (strcmp(fields[i].name, "user") == 0) { charLen = monGetRowElemCharLen(fields[i], (char *)row[i]); if (charLen < 0) { monError("failed to save slow_query info, reason: invalid row %s len, sql:%s", (char *)row[i], tsMonitor.sql); goto DONE; } - pos += snprintf(sql + pos, strlen(SQL_STR_FMT) + charLen + 1, ", "SQL_STR_FMT, (char *)row[i]); + if (pos < SQL_LENGTH) + pos += snprintf(sql + pos, SQL_LENGTH - pos, ", "SQL_STR_FMT, (char *)row[i]); } else if (strcmp(fields[i].name, "qid") == 0) { charLen = monGetRowElemCharLen(fields[i], (char *)row[i]); if (charLen < 0) { monError("failed to save slow_query info, reason: invalid row %s len, sql:%s", (char *)row[i], tsMonitor.sql); goto DONE; } - pos += snprintf(sql + pos, strlen(SQL_STR_FMT) + charLen + 1, ", "SQL_STR_FMT, (char *)row[i]); + if (pos < SQL_LENGTH) + pos += snprintf(sql + pos, SQL_LENGTH - pos, ", "SQL_STR_FMT, (char *)row[i]); } else if (strcmp(fields[i].name, "created_time") == 0) { int64_t create_time = *(int64_t *)row[i]; create_time = convertTimePrecision(create_time, TSDB_TIME_PRECISION_MILLI, TSDB_TIME_PRECISION_MICRO); - pos += snprintf(sql + pos, SQL_LENGTH, ", %" PRId64 "", create_time); + if (pos < SQL_LENGTH) + pos += snprintf(sql + pos, SQL_LENGTH - pos, ", %" PRId64 "", create_time); } else if (strcmp(fields[i].name, "time") == 0) { - pos += snprintf(sql + pos, SQL_LENGTH, ", %" PRId64 "", *(int64_t *)row[i]); + if (pos < SQL_LENGTH) + pos += snprintf(sql + pos, SQL_LENGTH - pos, ", %" PRId64 "", *(int64_t *)row[i]); } else if (strcmp(fields[i].name, "ep") == 0) { charLen = monGetRowElemCharLen(fields[i], (char *)row[i]); if (charLen < 0) { monError("failed to save slow_query info, reason: invalid row %s len, sql:%s", (char *)row[i], tsMonitor.sql); goto DONE; } - pos += snprintf(sql + pos, strlen(SQL_STR_FMT) + charLen + 1, ", "SQL_STR_FMT, (char *)row[i]); + if (pos < SQL_LENGTH) + pos += snprintf(sql + pos, SQL_LENGTH - pos, ", "SQL_STR_FMT, (char *)row[i]); } else if (strcmp(fields[i].name, "sql") == 0) { charLen = monGetRowElemCharLen(fields[i], (char *)row[i]); if (charLen < 0) { monError("failed to save slow_query info, reason: invalid row %s len, sql:%s", (char *)row[i], tsMonitor.sql); goto DONE; } - pos += snprintf(sql + pos, strlen(SQL_STR_FMT) + charLen + 2, ", "SQL_STR_FMT")", (char *)row[i]); + if (pos < SQL_LENGTH) + pos += snprintf(sql + pos, SQL_LENGTH - pos, ", "SQL_STR_FMT")", (char *)row[i]); } } } -- GitLab