libsepol: fix potential free of uninitialized pointer

When using "gcc -O2 -Wall -Werror" to compile libsepol, the following
error happens:

  services.c: In function 'constraint_expr_eval_reason':
  services.c:820:2: error: 'answer_list' may be used uninitialized in this
  function [-Werror=maybe-uninitialized]
    free(answer_list);
    ^

Indeed, because of a goto statement in constraint_expr_eval_reason
function, "free(answer_list)" can be called before answer_list has been
initialized.

Fix this error by moving the definition of answer_list to the beginning
of constraint_expr_eval_reason.

Acked-by: Steve Lawrence <slawrence@tresys.com>
libsemanage: Fix use of unitialized variable

"gcc -O2 -Wall -Werror" fails to compile seusers_local.c:

  seusers_local.c: In function 'semanage_seuser_modify_local':
  seusers_local.c:122:6: error: 'rc' may be used uninitialized in this
  function [-Werror=maybe-uninitialized]

It seems rc is not initialized when the call to semanage_seuser_clone
fails in semanage_seuser_modify_local.

Acked-by: Steve Lawrence <slawrence@tresys.com>
policycoreutils/hll/pp: fix potential use of uninitialized variable

"gcc -O2 -Wall -Werror" failed with two errors when building pp due to
the use of unitialized variables.

Acked-by: Steve Lawrence <slawrence@tresys.com>
policycoreutils/sandbox: fix debug build

Building from the root directory with "make DEBUG=1" enables -Wshadow
option.  This makes the compilation fails with the following error:

  cc -g3 -O0 -gdwarf-2 -fno-strict-aliasing -Wall -Wshadow -Werror -g
  -I/usr/include -DPACKAGE="\"policycoreutils\"" -Wall -Werror -Wextra
  -W   -c -o seunshare.o seunshare.c

  seunshare.c: In function 'spawn_command':
  seunshare.c:141:6: error: declaration of 'child' shadows a global declaration [-Werror=shadow]
    int child;
        ^
  seunshare.c:58:12: error: shadowed declaration is here [-Werror=shadow]
   static int child = 0;
              ^

Fix this error by renaming the "child" variable in spawn_command.

Acked-by: Steve Lawrence <slawrence@tresys.com>
policycoreutils/semodule_package: fix debug build

Building from the root directory with "make DEBUG=1" enables -Wshadow
option.  This makes the compilation fail with the following error:

  semodule_unpackage.c: In function 'usage':
  semodule_unpackage.c:17:25: error: declaration of 'progname' shadows a global declaration [-Werror=shadow]
   static void usage(char *progname)
                         ^
  semodule_unpackage.c:14:7: error: shadowed declaration is here [-Werror=shadow]
   char *progname = NULL;
         ^

Fix this error by no longer passing a global variable as a parameter to
usage function.

Acked-by: Steve Lawrence <slawrence@tresys.com>
policycoreutils/hll/pp: add printf format attribute to relevant functions

log_err, cil_printf and cil_println use printf formats to process their
arguments.  Use __attribute__((format(printf,...))) to make "gcc
-Wformat -Wformat-security" detect issues.

This detected this issue several times on a x86_64 system:

  format '%lx' expects argument of type 'long unsigned int', but
  argument has type 'uint32_t'

Fix this by introducing an explicit cast to unsigned long.

While at it, constify the format string argument of each function.

Acked-by: Steve Lawrence <slawrence@tresys.com>
checkpolicy: add printf format attribute to relevant functions

Once __attribute__ ((format(printf, 1, 2))) is added to yyerror2,
"gcc -Wformat -Wformat-security" shows some issues.  Fix them.

Acked-by: Steve Lawrence <slawrence@tresys.com>
checkpolicy: constify the message written by yyerror and yywarn

Acked-by: Steve Lawrence <slawrence@tresys.com>
libselinux: fix gcc -Wsign-compare warnings

Acked-by: Steve Lawrence <slawrence@tresys.com>
checkpolicy: fix gcc -Wsign-compare warnings

Acked-by: Steve Lawrence <slawrence@tresys.com>
libsepol: fix most gcc -Wwrite-strings warnings

gcc puts literal strings lie in read-only memory.  On x86_64, trying to
write to them triggers a segmentation fault.

To detect such issues at build time, variables holding a pointer to such
strings should be "const char*".  "gcc -Wwrite-strings" warns when using
non-const pointers to literal strings.

Remove gcc warnings by adding const to local variables and argumens of
internal functions.

This does *not* fix this warning:

  policydb_public.c:208:10: warning: passing argument 2 of 'hashtab_search' discards 'const' qualifier from pointer target type
    return (hashtab_search(p->p.p_classes.table, PACKET_CLASS_NAME) ==
            ^
  In file included from ../include/sepol/policydb/symtab.h:16:0,
                   from ../include/sepol/policydb/policydb.h:60,
                   from policydb_public.c:4:
  ../include/sepol/policydb/hashtab.h:98:24: note: expected 'hashtab_key_t' but argument is of type 'const char *'
  extern hashtab_datum_t hashtab_search(hashtab_t h, const hashtab_key_t k);
                         ^

Moreover the "const" word in hashtab_search prototype does not make the
second parameter "const char*" but "char* const".

Acked-by: Steve Lawrence <slawrence@tresys.com>
libsemanage: constify name and ext_lang parameters of semanage_module_install_hll

This fixes a warning from "gcc -Wwrite-strings", when
semanage_module_install_hll is called with "pp" as last parameter.

Acked-by: Steve Lawrence <slawrence@tresys.com>
libsemanage: fix gcc -Wwrite-strings warnings

Acked-by: Steve Lawrence <slawrence@tresys.com>
checkpolicy: fix most gcc -Wwrite-strings warnings

Acked-by: Steve Lawrence <slawrence@tresys.com>
policycoreutils/hll/pp: fix gcc -Wwrite-strings warnings

Acked-by: Steve Lawrence <slawrence@tresys.com>
policycoreutils: fix most gcc -Wwrite-strings warnings

Acked-by: Steve Lawrence <slawrence@tresys.com>
Fix gcc -Wstrict-prototypes warnings

In C, defining a function with () means "any number of parameters", not
"no parameter".  Use (void) instead where applicable and add unused
parameters when needed.

Acked-by: Steve Lawrence <slawrence@tresys.com>
checkpolicy: fix gcc -Wunused-variable warnings

Add __attribute__ ((unused)) to unused function parameters.

Acked-by: Steve Lawrence <slawrence@tresys.com>
libsepol/tests: fix gcc -Warray-bounds warning

  test-linker-roles.c: In function 'module_role_tests':
  test-linker-roles.c:147:7: error: array subscript is above array bounds
  [-Werror=array-bounds]
    decls[2] = (test_find_decl_by_sym(base, SYM_TYPES,"tag_g_m2"))->decl_id;
         ^

Acked-by: Steve Lawrence <slawrence@tresys.com>
Use $(PYTHON) instead of "python" in every Makefile

This fixes the build with "make PYTHON=python2" on systems where python
is python3.

For PYLIBVER and PYTHONLIBDIR definitions, I tested Python 2.5, 2.6, 2.7,
3.3 and 3.4.  For each of them, these commands print the expected result:

    python -c 'import sys;print("python%d.%d" % sys.version_info[0:2])'"
    python -c "from distutils.sysconfig import *;print(get_python_lib(1))"

Acked-by: Steve Lawrence <slawrence@tresys.com>