diff --git a/include/asterisk/strings.h b/include/asterisk/strings.h
index 529456c987ff15cbe38a88bc60b8a344edbdf799..bf3cf3236331ecc59a8ec3b1f48d628e65c0aa24 100644
--- a/include/asterisk/strings.h
+++ b/include/asterisk/strings.h
@@ -403,6 +403,16 @@ void ast_str_reset(struct ast_str *buf),
 }
 )
 
+/*! \brief Update the length of the buffer, after using ast_str merely as a buffer.
+ *  \param buf A pointer to the ast_str string.
+ */
+AST_INLINE_API(
+void ast_str_update(struct ast_str *buf),
+{
+	buf->__AST_STR_USED = strlen(buf->__AST_STR_STR);
+}
+)
+
 /*! \brief Trims trailing whitespace characters from an ast_str string.
  *  \param buf A pointer to the ast_str string.
  */
diff --git a/res/res_agi.c b/res/res_agi.c
index 926d4d95bc1d1d9cbdbf8c511b9615ca5f10d443..6151549e302dfc36a345b07b755f6e8bba161e4e 100644
--- a/res/res_agi.c
+++ b/res/res_agi.c
@@ -1861,6 +1861,7 @@ static int handle_dbget(struct ast_channel *chan, AGI *agi, int argc, char **arg
 
 	do {
 		res = ast_db_get(argv[2], argv[3], ast_str_buffer(buf), ast_str_size(buf));
+		ast_str_update(buf);
 		if (ast_str_strlen(buf) < ast_str_size(buf) - 1) {
 			break;
 		}