diff options
| author | Elliott Hughes <enh@google.com> | 2018-04-25 14:52:50 -0700 |
|---|---|---|
| committer | Elliott Hughes <enh@google.com> | 2018-04-25 14:55:48 -0700 |
| commit | 9f49508f368204657414ec0f2852f2a7abb5b4c5 (patch) | |
| tree | acc3a55ec97c42678f59cb0568b5d1cfad3664af /libcutils/sched_policy.cpp | |
| parent | 419ba9e2aadbfed3862d95265ce141d016c7ca69 (diff) | |
| download | system_core-9f49508f368204657414ec0f2852f2a7abb5b4c5.tar.gz system_core-9f49508f368204657414ec0f2852f2a7abb5b4c5.tar.bz2 system_core-9f49508f368204657414ec0f2852f2a7abb5b4c5.zip | |
Rewrite get_sched_policy_name for safety.
This way you'll get a build time error if you make the usual mistake of
adding to the enum but not adding an entry to the array.
Also improve the unit tests, and fix get_sched_policy_name's incorrect
behavior on invalid inputs.
Bug: N/A
Test: ran tests
Change-Id: Iefcb1ec9ef66267837da7a576c8be3d0cfb16cd0
Diffstat (limited to 'libcutils/sched_policy.cpp')
| -rw-r--r-- | libcutils/sched_policy.cpp | 13 |
1 files changed, 7 insertions, 6 deletions
diff --git a/libcutils/sched_policy.cpp b/libcutils/sched_policy.cpp index f72ec52d6..3fa548f78 100644 --- a/libcutils/sched_policy.cpp +++ b/libcutils/sched_policy.cpp @@ -25,6 +25,7 @@ #include <string.h> #include <unistd.h> +#include <android-base/macros.h> #include <log/log.h> /* Re-map SP_DEFAULT to the system default policy, and leave other values unchanged. @@ -460,16 +461,16 @@ int get_sched_policy(int /*tid*/, SchedPolicy* policy) { #endif -const char *get_sched_policy_name(SchedPolicy policy) -{ +const char* get_sched_policy_name(SchedPolicy policy) { policy = _policy(policy); - static const char* const strings[SP_CNT] = { + static const char* const kSchedPolicyNames[] = { [SP_BACKGROUND] = "bg", [SP_FOREGROUND] = "fg", [SP_SYSTEM] = " ", [SP_AUDIO_APP] = "aa", [SP_AUDIO_SYS] = "as", [SP_TOP_APP] = "ta", [SP_RT_APP] = "rt", [SP_RESTRICTED] = "rs", }; - if ((policy < SP_CNT) && (strings[policy] != NULL)) - return strings[policy]; - else + static_assert(arraysize(kSchedPolicyNames) == SP_CNT, "missing name"); + if (policy < SP_BACKGROUND || policy >= SP_CNT) { return "error"; + } + return kSchedPolicyNames[policy]; } |
