From 20ac944ea00dbef78574b37e490ebe66c9190c13 Mon Sep 17 00:00:00 2001 From: Yangster-mac Date: Mon, 8 Jan 2018 14:54:48 -0800 Subject: [PATCH] Handle null string in jni and c++ stats-log-api interfaces. Test: statsd unit test passed. Change-Id: I4a475d6fcc0b4a0293450bc0ccdd718b362c498b --- tools/stats_log_api_gen/main.cpp | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/tools/stats_log_api_gen/main.cpp b/tools/stats_log_api_gen/main.cpp index 89749fb52bb4d..bbe6d63073c1c 100644 --- a/tools/stats_log_api_gen/main.cpp +++ b/tools/stats_log_api_gen/main.cpp @@ -166,7 +166,15 @@ static int write_stats_log_cpp(FILE *out, const Atoms &atoms, attributionDecl.fields.front().name.c_str()); fprintf(out, " event.begin();\n"); for (const auto &chainField : attributionDecl.fields) { - fprintf(out, " event << %s[i];\n", chainField.name.c_str()); + if (chainField.javaType == JAVA_TYPE_STRING) { + fprintf(out, " if (%s[i] != NULL) {\n", chainField.name.c_str()); + fprintf(out, " event << %s[i];\n", chainField.name.c_str()); + fprintf(out, " } else {\n"); + fprintf(out, " event << \"\";\n"); + fprintf(out, " }\n"); + } else { + fprintf(out, " event << %s[i];\n", chainField.name.c_str()); + } } fprintf(out, " event.end();\n"); fprintf(out, " }\n"); @@ -589,13 +597,18 @@ write_stats_log_jni(FILE* out, const Atoms& atoms, const AtomDecl &attributionDe fprintf(out, " jstring jstr = " "(jstring)env->GetObjectArrayElement(%s, i);\n", chainField.name.c_str()); - fprintf(out, " ScopedUtfChars* scoped_%s = " + fprintf(out, " if (jstr == NULL) {\n"); + fprintf(out, " %s_vec.push_back(NULL);\n", + chainField.name.c_str()); + fprintf(out, " } else {\n"); + fprintf(out, " ScopedUtfChars* scoped_%s = " "new ScopedUtfChars(env, jstr);\n", chainField.name.c_str()); - fprintf(out, " %s_vec.push_back(scoped_%s->c_str());\n", + fprintf(out, " %s_vec.push_back(scoped_%s->c_str());\n", chainField.name.c_str(), chainField.name.c_str()); - fprintf(out, " scoped_%s_vec.push_back(scoped_%s);\n", + fprintf(out, " scoped_%s_vec.push_back(scoped_%s);\n", chainField.name.c_str(), chainField.name.c_str()); + fprintf(out, " }\n"); fprintf(out, " }\n"); } fprintf(out, "\n"); @@ -648,7 +661,7 @@ write_stats_log_jni(FILE* out, const Atoms& atoms, const AtomDecl &attributionDe fprintf(out, " env->ReleaseIntArrayElements(%s, %s_array, 0);\n", chainField.name.c_str(), chainField.name.c_str()); } else if (chainField.javaType == JAVA_TYPE_STRING) { - fprintf(out, " for (size_t i = 0; i < %s_length; ++i) {\n", + fprintf(out, " for (size_t i = 0; i < scoped_%s_vec.size(); ++i) {\n", chainField.name.c_str()); fprintf(out, " delete scoped_%s_vec[i];\n", chainField.name.c_str()); fprintf(out, " }\n");