No OOM in enc_canonize()

Fix a `return FAIL` that should be `return NULL` in `enc_locale()`
This commit is contained in:
Felipe Oliveira Carvalho
2014-05-30 23:19:02 -03:00
parent 8234f2839f
commit 81ca5ff126
5 changed files with 13 additions and 30 deletions

View File

@@ -3012,8 +3012,6 @@ void ex_scriptencoding(exarg_T *eap)
if (*eap->arg != NUL) { if (*eap->arg != NUL) {
name = enc_canonize(eap->arg); name = enc_canonize(eap->arg);
if (name == NULL) /* out of memory */
return;
} else } else
name = eap->arg; name = eap->arg;

View File

@@ -2087,10 +2087,7 @@ void set_forced_fenc(exarg_T *eap)
{ {
if (eap->force_enc != 0) { if (eap->force_enc != 0) {
char_u *fenc = enc_canonize(eap->cmd + eap->force_enc); char_u *fenc = enc_canonize(eap->cmd + eap->force_enc);
set_string_option_direct((char_u *)"fenc", -1, fenc, OPT_FREE|OPT_LOCAL, 0);
if (fenc != NULL)
set_string_option_direct((char_u *)"fenc", -1,
fenc, OPT_FREE|OPT_LOCAL, 0);
free(fenc); free(fenc);
} }
} }

View File

@@ -84,6 +84,7 @@
#include "nvim/charset.h" #include "nvim/charset.h"
#include "nvim/cursor.h" #include "nvim/cursor.h"
#include "nvim/fileio.h" #include "nvim/fileio.h"
#include "nvim/func_attr.h"
#include "nvim/memline.h" #include "nvim/memline.h"
#include "nvim/message.h" #include "nvim/message.h"
#include "nvim/misc1.h" #include "nvim/misc1.h"
@@ -3308,24 +3309,23 @@ char_u * enc_skip(char_u *p)
* Find the canonical name for encoding "enc". * Find the canonical name for encoding "enc".
* When the name isn't recognized, returns "enc" itself, but with all lower * When the name isn't recognized, returns "enc" itself, but with all lower
* case characters and '_' replaced with '-'. * case characters and '_' replaced with '-'.
* Returns an allocated string. NULL for out-of-memory. * Returns an allocated string.
*/ */
char_u * enc_canonize(char_u *enc) char_u *enc_canonize(char_u *enc) FUNC_ATTR_NONNULL_RET
{ {
char_u *r;
char_u *p, *s; char_u *p, *s;
int i; int i;
if (STRCMP(enc, "default") == 0) { if (STRCMP(enc, "default") == 0) {
/* Use the default encoding as it's found by set_init_1(). */ /* Use the default encoding as it's found by set_init_1(). */
r = get_encoding_default(); char_u *r = get_encoding_default();
if (r == NULL) if (r == NULL)
r = (char_u *)"latin1"; r = (char_u *)"latin1";
return vim_strsave(r); return vim_strsave(r);
} }
/* copy "enc" to allocated memory, with room for two '-' */ /* copy "enc" to allocated memory, with room for two '-' */
r = xmalloc(STRLEN(enc) + 3); char_u *r = xmalloc(STRLEN(enc) + 3);
/* Make it all lower case and replace '_' with '-'. */ /* Make it all lower case and replace '_' with '-'. */
p = r; p = r;
for (s = enc; *s != NUL; ++s) { for (s = enc; *s != NUL; ++s) {
@@ -3411,7 +3411,7 @@ char_u * enc_locale()
s = (char *)os_getenv("LANG"); s = (char *)os_getenv("LANG");
if (s == NULL || *s == NUL) if (s == NULL || *s == NUL)
return FAIL; return NULL;
/* The most generic locale format is: /* The most generic locale format is:
* language[_territory][.codeset][@modifier][+special][,[sponsor][_revision]] * language[_territory][.codeset][@modifier][+special][,[sponsor][_revision]]

View File

@@ -3971,10 +3971,8 @@ did_set_string_option (
if (errmsg == NULL) { if (errmsg == NULL) {
/* canonize the value, so that STRCMP() can be used on it */ /* canonize the value, so that STRCMP() can be used on it */
p = enc_canonize(*varp); p = enc_canonize(*varp);
if (p != NULL) { free(*varp);
free(*varp); *varp = p;
*varp = p;
}
if (varp == &p_enc) { if (varp == &p_enc) {
errmsg = mb_init(); errmsg = mb_init();
redraw_titles(); redraw_titles();
@@ -4000,18 +3998,8 @@ did_set_string_option (
} else if (varp == &p_penc) { } else if (varp == &p_penc) {
/* Canonize printencoding if VIM standard one */ /* Canonize printencoding if VIM standard one */
p = enc_canonize(p_penc); p = enc_canonize(p_penc);
if (p != NULL) { free(p_penc);
free(p_penc); p_penc = p;
p_penc = p;
} else {
/* Ensure lower case and '-' for '_' */
for (s = p_penc; *s != NUL; s++) {
if (*s == '_')
*s = '-';
else
*s = TOLOWER_ASC(*s);
}
}
} else if (varp == &curbuf->b_p_keymap) { } else if (varp == &curbuf->b_p_keymap) {
/* load or unload key mapping tables */ /* load or unload key mapping tables */
errmsg = keymap_init(); errmsg = keymap_init();

View File

@@ -4438,7 +4438,7 @@ static afffile_T *spell_read_aff(spellinfo_T *spin, char_u *fname)
if (is_aff_rule(items, itemcnt, "SET", 2) && aff->af_enc == NULL) { if (is_aff_rule(items, itemcnt, "SET", 2) && aff->af_enc == NULL) {
// Setup for conversion from "ENC" to 'encoding'. // Setup for conversion from "ENC" to 'encoding'.
aff->af_enc = enc_canonize(items[1]); aff->af_enc = enc_canonize(items[1]);
if (aff->af_enc != NULL && !spin->si_ascii if (!spin->si_ascii
&& convert_setup(&spin->si_conv, aff->af_enc, && convert_setup(&spin->si_conv, aff->af_enc,
p_enc) == FAIL) p_enc) == FAIL)
smsg((char_u *)_("Conversion in %s not supported: from %s to %s"), smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
@@ -5978,7 +5978,7 @@ static int spell_read_wordfile(spellinfo_T *spin, char_u *fname)
// Setup for conversion to 'encoding'. // Setup for conversion to 'encoding'.
line += 9; line += 9;
enc = enc_canonize(line); enc = enc_canonize(line);
if (enc != NULL && !spin->si_ascii if (!spin->si_ascii
&& convert_setup(&spin->si_conv, enc, && convert_setup(&spin->si_conv, enc,
p_enc) == FAIL) p_enc) == FAIL)
smsg((char_u *)_("Conversion in %s not supported: from %s to %s"), smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),