if_cscope: Fix conversion warnings when char defaults to unsigned

../src/nvim/if_cscope.c: In function 'cs_read_prompt':
    ../src/nvim/if_cscope.c:1771:47: warning: comparison is always true due to limited range of data type [-Wtype-limits]
         while ((ch = (char)getc(csinfo[i].fr_fp)) != EOF && ch != CSCOPE_PROMPT[0])
                                                   ^~
    ../src/nvim/if_cscope.c:1804:14: warning: comparison is always false due to limited range of data type [-Wtype-limits]
           if (ch == EOF) {
                  ^~
    ../src/nvim/if_cscope.c:1816:14: warning: negative integer implicitly converted to unsigned type [-Wsign-conversion]
             ch = EOF;
                  ^~~
    ../src/nvim/if_cscope.c:1821:12: warning: comparison is always false due to limited range of data type [-Wtype-limits]
         if (ch == EOF)
                ^~

Since EOF is -1, it will be converted to a large unsigned value to
compare with unsigned char and never match.  Use an int to store the
return from getc so we can safely compare it and, once known to be
valid, cast it to char when storing it into buf.

Signed-off-by: James McCoy <jamessan@jamessan.com>
This commit is contained in:
James McCoy
2016-10-09 20:56:51 -04:00
parent 4192c411a8
commit 8fd12805d7

View File

@@ -1761,7 +1761,7 @@ static void cs_print_tags_priv(char **matches, char **cntxts,
*/ */
static int cs_read_prompt(size_t i) static int cs_read_prompt(size_t i)
{ {
char ch; int ch;
char *buf = NULL; /* buffer for possible error message from cscope */ char *buf = NULL; /* buffer for possible error message from cscope */
size_t bufpos = 0; size_t bufpos = 0;
char *cs_emsg = _("E609: Cscope error: %s"); char *cs_emsg = _("E609: Cscope error: %s");
@@ -1774,7 +1774,7 @@ static int cs_read_prompt(size_t i)
size_t maxlen = IOSIZE - cs_emsg_len; size_t maxlen = IOSIZE - cs_emsg_len;
for (;; ) { for (;; ) {
while ((ch = (char)getc(csinfo[i].fr_fp)) != EOF && ch != CSCOPE_PROMPT[0]) while ((ch = getc(csinfo[i].fr_fp)) != EOF && ch != CSCOPE_PROMPT[0])
/* if there is room and char is printable */ /* if there is room and char is printable */
if (bufpos < maxlen - 1 && vim_isprintc(ch)) { if (bufpos < maxlen - 1 && vim_isprintc(ch)) {
// lazy buffer allocation // lazy buffer allocation
@@ -1783,7 +1783,7 @@ static int cs_read_prompt(size_t i)
} }
{ {
/* append character to the message */ /* append character to the message */
buf[bufpos++] = ch; buf[bufpos++] = (char)ch;
buf[bufpos] = NUL; buf[bufpos] = NUL;
if (bufpos >= epromptlen if (bufpos >= epromptlen
&& strcmp(&buf[bufpos - epromptlen], eprompt) == 0) { && strcmp(&buf[bufpos - epromptlen], eprompt) == 0) {