Skip to content

Commit 025f433

Browse files
committed
Bug #17218692 Correct snprintf buffer size (no need for -1, because snprintf puts allways a \0 at string end)
Add unit test with this bug case.
1 parent cf51692 commit 025f433

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

driver/mysql_ps_resultset.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -980,9 +980,9 @@ MySQL_Prepared_ResultSet::getString(const uint32_t columnIndex) const
980980
char buf[18];
981981
MYSQL_TIME * t = static_cast<MYSQL_TIME *>(result_bind->rbind[columnIndex - 1].buffer);
982982
if (t->second_part) {
983-
snprintf(buf, sizeof(buf) - 1, "%s%02d:%02d:%02d.%06lu", t->neg? "-":"", t->hour, t->minute, t->second, t->second_part);
983+
snprintf(buf, sizeof(buf), "%s%02d:%02d:%02d.%06lu", t->neg? "-":"", t->hour, t->minute, t->second, t->second_part);
984984
} else {
985-
snprintf(buf, sizeof(buf) - 1, "%s%02d:%02d:%02d", t->neg? "-":"", t->hour, t->minute, t->second);
985+
snprintf(buf, sizeof(buf), "%s%02d:%02d:%02d", t->neg? "-":"", t->hour, t->minute, t->second);
986986
}
987987
CPP_INFO_FMT("It's a time %s", buf);
988988
return sql::SQLString(buf);

test/unit/bugs/bugs.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,5 +733,41 @@ void bugs::bug66235()
733733
}
734734

735735

736+
void bugs::bug17218692()
737+
{
738+
logMsg("bugs::bug17218692");
739+
try
740+
{
741+
742+
stmt->execute("DROP TABLE IF EXISTS bug17218692");
743+
stmt->execute("CREATE TABLE bug17218692(c1 time(6))");
744+
stmt->execute("INSERT INTO bug17218692 VALUES('-838:59:58.987657')");
745+
746+
res.reset(stmt->executeQuery("select * from bug17218692"));
747+
res->next();
748+
749+
std::stringstream log;
750+
log<<"["<<res->getString(1)<<"]";
751+
752+
ASSERT_EQUALS(log.str(), "[-838:59:58.987657]");
753+
754+
pstmt.reset(con->prepareStatement("select * from bug17218692 "));
755+
res.reset(pstmt->executeQuery());
756+
res->next();
757+
758+
std::stringstream log2;
759+
log2 << "["<<res->getString(1)<<"]";
760+
ASSERT_EQUALS(log.str(), log2.str());
761+
762+
}
763+
catch (sql::SQLException & e)
764+
{
765+
FAIL("Exception thrown");
766+
throw;
767+
}
768+
769+
770+
}
771+
736772
} /* namespace regression */
737773
} /* namespace testsuite */

test/unit/bugs/bugs.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ class bugs : public unit_fixture
6161
TEST_CASE(bug19938873_stmt);
6262
TEST_CASE(bug68523);
6363
TEST_CASE(bug66235);
64+
TEST_CASE(bug17218692);
6465
}
6566

6667
/**
@@ -103,6 +104,8 @@ class bugs : public unit_fixture
103104
void bug68523();
104105

105106
void bug66235();
107+
108+
void bug17218692();
106109
};
107110

108111
REGISTER_FIXTURE(bugs);

0 commit comments

Comments
 (0)