Fix file leak on malloc error in XlcDL.c:resolve_object()

File Leak: Leaked File fp
   at line 219 of lib/libX11/src/xlibi18n/XlcDL.c in function 'resolve_object'.
      fp initialized at line 198 with fopen

[ This bug was found by the Parfait 1.2.0 bug checking tool.
  http://labs.oracle.com/pls/apex/f?p=labs:49:::::P49_PROJECT_ID:13 ]

Signed-off-by: Alan Coopersmith <alan.coopersmith@oracle.com>
Reviewed-by: Matthieu Herrb <matthieu.herrb@laas.fr>
lcfile: skip over any null entries in args list

Previous code seemed to assume that printf("%s", NULL) would result
in a 0-length string, not "(null)" or similar, but since there's no
point looking for files in "(null)/filepath...", instead we just
skip over NULL entries in search paths when generating file names.

In the *DirName() functions, this effectively just moves the "bail on
NULL in arg[i]" check up from the later code that assigned it to targetdir
and then bailed if that was NULL.

Not sure how there ever could be a NULL in arg[i], given the current
implementation of XlcParsePath, but it's easy enough to check once and
reject up front instead of on every reference.

Signed-off-by: Alan Coopersmith <alan.coopersmith@oracle.com>
xlibi18n: convert sprintf calls to snprintf

Signed-off-by: Alan Coopersmith <alan.coopersmith@oracle.com>
Reviewed-by: Matthieu Herrb <matthieu.herrb@laas.fr>
ximcp/imRm.c: convert sprintf calls to snprintf

Signed-off-by: Alan Coopersmith <alan.coopersmith@oracle.com>
Reviewed-by: Matthieu Herrb <matthieu.herrb@laas.fr>
omGeneric.c: convert sprintf calls to snprintf

Signed-off-by: Alan Coopersmith <alan.coopersmith@oracle.com>
Reviewed-by: Matthieu Herrb <matthieu.herrb@laas.fr>
Avoid memory leak/corruption if realloc fails in XlcDL.c:resolve_object()

Previously, if realloc failed to increase the size, we'd still
record that we had allocated the larger size, but the pointer
to it would be NULL, causing future calls to be broken, and the
previous allocation to be lost/leaked.

Signed-off-by: Alan Coopersmith <alan.coopersmith@oracle.com>
Reviewed-by: Matthieu Herrb <matthieu.herrb@laas.fr>
lcDB.c: ensure buffer size is updated correctly if realloc fails

Signed-off-by: Alan Coopersmith <alan.coopersmith@oracle.com>
Avoid memory leak/corruption if realloc fails in imLcPrs.c:parseline()

Signed-off-by: Alan Coopersmith <alan.coopersmith@oracle.com>
Avoid memory leak/corruption if realloc fails in Xregion.h:MEMCHECK macro

Signed-off-by: Alan Coopersmith <alan.coopersmith@oracle.com>
miRegionCopy(): handle realloc failure better

Zero out the region size when freeing the region so callers don't think
there's anything there.   (Pointer is already set to NULL from the realloc
result itself.)  Return 0 to the callers, and have them cascade that back
to their callers to indicate failure, instead of their usual return value
of 1 on success.

Signed-off-by: Alan Coopersmith <alan.coopersmith@oracle.com>
miRegionOp(): ensure region size is not updated if realloc fails

This function performs operations on a region, and when finished,
checks to see if it should compact the rectangle list.  If the
number of rectangles for which memory is allocated in the list is
more than twice the number used, it tries to shrink.   realloc()
should not fail in this case, but if it does, might as well keep
the correct value for the number of allocated rectangles, so we
don't try to grow it unnecessarily later if adding to the region.

Signed-off-by: Alan Coopersmith <alan.coopersmith@oracle.com>
Delete unused XKB_INSURE_SIZE macro from XKBlibint.h

Signed-off-by: Alan Coopersmith <alan.coopersmith@oracle.com>
Reviewed-by: Matthieu Herrb <matthieu.herrb@laas.fr>
init_om: remove unneeded extra copy of string to local buffer

Strings from the supported_charset_list[] were being copied one by
one to a stack buffer, and then strdup called on that buffer.

Instead, just strdup the original string, without the local copy,
and use a more traditional for loop, so it's easier to figure out
what the code is doing (cleaning up a gcc const-cast warning in
the process).

Signed-off-by: Alan Coopersmith <alan.coopersmith@oracle.com>
Reviewed-by: Matthieu Herrb <matthieu.herrb@laas.fr>
Constify lc_name argument to _XlcLocaleDirName() & _XlcLocaleLibDirName()

Makes code considerably less crufty and clears gcc warnings:
XlcDL.c: In function '_XlcDynamicLoad':
XlcDL.c:384:44: warning: cast discards '__attribute__((const))' qualifier
 from pointer target type [-Wcast-qual]
XlcDL.c:386:51: warning: cast discards '__attribute__((const))' qualifier
 from pointer target type [-Wcast-qual]

Signed-off-by: Alan Coopersmith <alan.coopersmith@oracle.com>
Fix const handling in XSetLocaleModifiers

Instead of reusing the input parameter to store the output, make a
result variable instead, so that there's less const confusion.

Fixes gcc warnings:
lcWrap.c: In function 'XSetLocaleModifiers':
lcWrap.c:87:18: warning: cast discards '__attribute__((const))' qualifier from pointer target type [-Wcast-qual]
lcWrap.c:91:25: warning: cast discards '__attribute__((const))' qualifier from pointer target type [-Wcast-qual]
lcWrap.c:93:12: warning: cast discards '__attribute__((const))' qualifier from pointer target type [-Wcast-qual]

Signed-off-by: Alan Coopersmith <alan.coopersmith@oracle.com>
xlibi18n: Fix a bunch of const cast warnings

Add const qualifiers to casts where needed, remove other casts that
are no longer needed.

Signed-off-by: Alan Coopersmith <alan.coopersmith@oracle.com>
i18n modules: Fix some const cast warnings

imRm.c: In function '_XimSetICMode':
imRm.c:2419:37: warning: cast discards '__attribute__((const))' qualifier from pointer target type [-Wcast-qual]
imRm.c:2420:30: warning: cast discards '__attribute__((const))' qualifier from pointer target type [-Wcast-qual]

lcGenConv.c: In function 'byteM_parse_codeset':
lcGenConv.c:345:13: warning: cast discards '__attribute__((const))' qualifier from pointer target type [-Wcast-qual]

Signed-off-by: Alan Coopersmith <alan.coopersmith@oracle.com>
xlibi18n: fix argsize argument to _XlcParsePath

The array is defined as having NUM_LOCALEDIR entries, so use that
instead of hardcoded 256 value (the other two calls already did this).

Reported by parfait:
   Buffer overflow (CWE 120): In pointer dereference of argv[argc] with index argc
      Pointer size is 64 elements (of 8 bytes each), index is 255
        at line 82 of src/xlibi18n/lcFile.c in function 'parse_line'.
        called at line 178 in function '_XlcParsePath' with argv = argv.
        called at line 722 in function '_XlcLocaleLibDirName' with argv = args, argsize = 256.
        at line 82 of src/xlibi18n/lcFile.c in function 'parse_line'.
        called at line 178 in function '_XlcParsePath' with argv = argv.
        called at line 638 in function '_XlcLocaleDirName' with argv = args, argsize = 256.

[ This bug was found by the Parfait 1.2.0 bug checking tool.
  http://labs.oracle.com/pls/apex/f?p=labs:49:::::P49_PROJECT_ID:13 ]

Signed-off-by: Alan Coopersmith <alan.coopersmith@oracle.com>
Remove even more casts of return values from Xmalloc/Xrealloc

Signed-off-by: Alan Coopersmith <alan.coopersmith@oracle.com>
Don't cast sizeof() results to unsigned when passing to Xmalloc/Xcalloc

sizeof() returns size_t, malloc() & calloc() expect sizes in size_t,
don't strip down to unsigned int and re-expand unnecessarily.

Signed-off-by: Alan Coopersmith <alan.coopersmith@oracle.com>