mirror of
https://github.com/neovim/neovim.git
synced 2025-02-25 18:55:25 -06:00
coverity/13689: Check file header with memcmp
Not that it is actually useful (would fail in any case), but should fix coverity report.
This commit is contained in:
parent
8f75b67c07
commit
35584594f5
@ -225,6 +225,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <wctype.h>
|
#include <wctype.h>
|
||||||
|
#include <strings.h>
|
||||||
|
|
||||||
#include "nvim/vim.h"
|
#include "nvim/vim.h"
|
||||||
#include "nvim/spell_defs.h"
|
#include "nvim/spell_defs.h"
|
||||||
@ -529,6 +530,26 @@ typedef struct spellinfo_S {
|
|||||||
} \
|
} \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
|
/// Check that spell file starts with a magic string
|
||||||
|
///
|
||||||
|
/// Does not check for version of the file.
|
||||||
|
///
|
||||||
|
/// @param fd File to check.
|
||||||
|
///
|
||||||
|
/// @return 0 in case of success, SP_TRUNCERROR if file contains not enough
|
||||||
|
/// bytes, SP_FORMERROR if it does not match magic string and
|
||||||
|
/// SP_OTHERERROR if reading file failed.
|
||||||
|
static inline int spell_check_magic_string(FILE *const fd)
|
||||||
|
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_ALWAYS_INLINE
|
||||||
|
{
|
||||||
|
char buf[VIMSPELLMAGICL];
|
||||||
|
SPELL_READ_BYTES(buf, VIMSPELLMAGICL, fd);
|
||||||
|
if (memcmp(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0) {
|
||||||
|
return SP_FORMERROR;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
// Load one spell file and store the info into a slang_T.
|
// Load one spell file and store the info into a slang_T.
|
||||||
//
|
//
|
||||||
// This is invoked in three ways:
|
// This is invoked in three ways:
|
||||||
@ -549,7 +570,6 @@ spell_load_file (
|
|||||||
)
|
)
|
||||||
{
|
{
|
||||||
FILE *fd;
|
FILE *fd;
|
||||||
char_u buf[VIMSPELLMAGICL];
|
|
||||||
char_u *p;
|
char_u *p;
|
||||||
int n;
|
int n;
|
||||||
int len;
|
int len;
|
||||||
@ -592,12 +612,20 @@ spell_load_file (
|
|||||||
sourcing_lnum = 0;
|
sourcing_lnum = 0;
|
||||||
|
|
||||||
// <HEADER>: <fileID>
|
// <HEADER>: <fileID>
|
||||||
for (size_t i = 0; i < VIMSPELLMAGICL; i++) {
|
const int scms_ret = spell_check_magic_string(fd);
|
||||||
buf[i] = getc(fd); // <fileID>
|
switch (scms_ret) {
|
||||||
}
|
case SP_FORMERROR:
|
||||||
if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0) {
|
case SP_TRUNCERROR: {
|
||||||
EMSG(_("E757: This does not look like a spell file"));
|
emsgf(_("E757: This does not look like a spell file"));
|
||||||
goto endFAIL;
|
goto endFAIL;
|
||||||
|
}
|
||||||
|
case SP_OTHERERROR: {
|
||||||
|
emsgf(_("E5042: Failed to read spell file %s: %s"),
|
||||||
|
fname, strerror(ferror(fd)));
|
||||||
|
}
|
||||||
|
case 0: {
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
c = getc(fd); // <versionnr>
|
c = getc(fd); // <versionnr>
|
||||||
if (c < VIMSPELLVERSION) {
|
if (c < VIMSPELLVERSION) {
|
||||||
|
@ -97,4 +97,12 @@ describe('spellfile', function()
|
|||||||
eq('Vim(set):E759: Format error in spell file',
|
eq('Vim(set):E759: Format error in spell file',
|
||||||
exc_exec('set spell'))
|
exc_exec('set spell'))
|
||||||
end)
|
end)
|
||||||
|
it('errors out when spell header contains NUL bytes', function()
|
||||||
|
meths.set_option('runtimepath', testdir)
|
||||||
|
write_file(testdir .. '/spell/en.ascii.spl',
|
||||||
|
spellheader:sub(1, -3) .. '\000\000')
|
||||||
|
meths.set_option('spelllang', 'en')
|
||||||
|
eq('Vim(set):E757: This does not look like a spell file',
|
||||||
|
exc_exec('set spell'))
|
||||||
|
end)
|
||||||
end)
|
end)
|
||||||
|
Loading…
Reference in New Issue
Block a user