Commit Graph

898 Commits

Author SHA1 Message Date
Nicolas Hillegeer
014febef22 coverity: fix BUFFER_SIZE_WARNING with str{n,l}cpy
Relates to issue #760

These coverity warnings are of the form:

>>>     CID 62602:  Buffer not null terminated  (BUFFER_SIZE_WARNING)
>>>     Calling strncpy with a maximum size argument of 256 bytes...

This is caused by strncpy not alway NULL-terminated the destination buffer
(for example in the case where strlen(src) >= size(dst)). It's better to
replace that with (x)strlcpy, which always NULL-terminates.

Most of these are related to the set_api_error macro, which uses strncpy.
The error struct is used (for example) in msgpack_rpc_error, where strlen is
executed on it, so it needs to be NULL-terminated. (x)strlcpy, unlike
strncpy, always NULL-terminates the destination buffer.

Relevant parts of the coverity report:

*** CID 62602:  Buffer not null terminated  (BUFFER_SIZE_WARNING)
/src/nvim/api/vim.c: 236 in vim_set_current_buffer()
230         if (try_end(err)) {
231           return;
232         }
233
234         char msg[256];
235         snprintf(msg, sizeof(msg),
              "failed to switch to buffer %d", (int)buffer);
>>>     CID 62602:  Buffer not null terminated  (BUFFER_SIZE_WARNING)
>>>     Calling strncpy with a maximum size argument of 256 bytes on
>>>     destination array "err->msg" of size 256 bytes might leave the
>>>     destination string unterminated.
236         set_api_error(msg, err);
237         return;
238       }
239
240       try_end(err);
241     }

*** CID 62603:  Buffer not null terminated  (BUFFER_SIZE_WARNING)
/src/nvim/api/private/helpers.c: 70 in try_end()
64       } else if (msg_list != NULL && *msg_list != NULL) {
65         int should_free;
66         char *msg = (char *)get_exception_string(*msg_list,
67                                                  ET_ERROR,
68                                                  NULL,
69                                                  &should_free);
>>>     CID 62603:  Buffer not null terminated  (BUFFER_SIZE_WARNING)
>>>     Calling strncpy with a maximum size argument of 256 bytes on
>>>     destination array "err->msg" of size 256 bytes might leave the
>>>     destination string unterminated.
70         strncpy(err->msg, msg, sizeof(err->msg));
71         err->set = true;
72         free_global_msglist();
73
74         if (should_free) {
75           free(msg);
/src/nvim/api/private/helpers.c: 78 in try_end()
72         free_global_msglist();
73
74         if (should_free) {
75           free(msg);
76         }
77       } else if (did_throw) {
>>>     CID 62603:  Buffer not null terminated  (BUFFER_SIZE_WARNING)
>>>     Calling strncpy with a maximum size argument of 256 bytes on
>>>     destination array "err->msg" of size 256 bytes might leave the
>>>     destination string unterminated.
78         set_api_error((char *)current_exception->value, err);
79       }
80
81       return err->set;
82     }
83

*** CID 62604:  Buffer not null terminated  (BUFFER_SIZE_WARNING)
/src/nvim/api/private/helpers.c: 592 in set_option_value_err()
586                                              opt_flags)))
587       {
588         if (try_end(err)) {
589           return;
590         }
591
>>>     CID 62604:  Buffer not null terminated  (BUFFER_SIZE_WARNING)
>>>     Calling strncpy with a maximum size argument of 256 bytes on
>>>     destination array "err->msg" of size 256 bytes might leave the
>>>     destination string unterminated.
592         set_api_error(errmsg, err);
593       }

*** CID 62605:  Buffer not null terminated  (BUFFER_SIZE_WARNING)
/src/nvim/os/server.c: 114 in server_start()
108       if (addr_len > sizeof(ip) - 1) {
109         // Maximum length of a ip address buffer is 15(eg: 255.255.255.255)
110         addr_len = sizeof(ip);
111       }
112
113       // Extract the address part
>>>     CID 62605:  Buffer not null terminated  (BUFFER_SIZE_WARNING)
>>>     Calling strncpy with a maximum size argument of 16 bytes on
>>>     destination array "ip" of size 16 bytes might leave the destination
>>>     string unterminated.
114       strncpy(ip, addr, addr_len);
115
116       int port = NEOVIM_DEFAULT_TCP_PORT;
117
118       if (*ip_end == ':') {
119         char *port_end;
/src/nvim/os/server.c: 88 in server_start()
82
83     void server_start(char *endpoint, ChannelProtocol prot)
84     {
85       char addr[ADDRESS_MAX_SIZE];
86
87       // Trim to `ADDRESS_MAX_SIZE`
>>>     CID 62605:  Buffer not null terminated  (BUFFER_SIZE_WARNING)
>>>     Calling strncpy with a maximum size argument of 256 bytes on
>>>     destination array "addr" of size 256 bytes might leave the
>>>     destination string unterminated.
88       strncpy(addr, endpoint, sizeof(addr));
89
90       // Check if the server already exists
91       if (map_has(cstr_t)(servers, addr)) {
92         EMSG2("Already listening on %s", addr);
93         return;

*** CID 62606:  Buffer not null terminated  (BUFFER_SIZE_WARNING)
/src/nvim/os/server.c: 186 in server_stop()
180     void server_stop(char *endpoint)
181     {
182       Server *server;
183       char addr[ADDRESS_MAX_SIZE];
184
185       // Trim to `ADDRESS_MAX_SIZE`
>>>     CID 62606:  Buffer not null terminated  (BUFFER_SIZE_WARNING)
>>>     Calling strncpy with a maximum size argument of 256 bytes on
>>>     destination array "addr" of size 256 bytes might leave the
>>>     destination string unterminated.
187
188       if ((server = map_get(cstr_t)(servers, addr)) == NULL) {
189         EMSG2("Not listening on %s", addr);
190         return;
191       }
2014-05-26 13:08:45 -03:00
Nicolas Hillegeer
a50a34f472 memory: add xstrlcpy
Less "blow a hole in your foot" than strncpy. As also indicated by coverity.
Implementation inspired by the linux kernel (very similar to OSX's Libc
implementation as well).
2014-05-26 13:08:45 -03:00
Justin M. Keyes
3a68a4861a Merge #733 'Simplify edit.c functions replace_{push,pop}.' 2014-05-24 04:57:25 -04:00
oni-link
49306cfa02 Simplify edit.c functions replace_{push,pop}.
* Replace xmalloc (+memmove) with xrealloc
* Code style adjustments.
* Remove obsolete TODO comment
2014-05-24 04:55:10 -04:00
Rainer Borene
4464732503 Add Reddit link to README. ref #704 2014-05-24 01:53:04 -04:00
Justin M. Keyes
f75d4bc3e1 Merge pull request #757 from elmart/remove-long_u
Remove project-specific integer types: long_u. (1)
2014-05-24 01:47:52 -04:00
Eliseo Martínez
f179081fc8 Remove long_u: hashtab: Enable clint: Add to clint.
- Add hashtab.[ch] to clint file.
- Fix clint errors.
2014-05-24 01:17:51 +02:00
Eliseo Martínez
ffed9bfae9 Remove long_u: hashtab: Enable clint: Sort clint file. 2014-05-24 01:17:50 +02:00
Eliseo Martínez
0509556b93 Remove long_u: hashtab: Refactor other types.
Current type of some other parameters/variables can be improved:
- hashtab_T         : ht_error : int  -> bool.
- hash_clear_all()  : off      : int  -> unsigned int.
- hash_clear_all()  : todo     : long -> size_t.
- hash_may_resize() : todo     : int  -> size_t.
2014-05-24 01:17:49 +02:00
Eliseo Martínez
ec89761e8a Remove long_u: hashtab: Refactor long_u type.
hashtab.h:
- hash_T: long_u -> size_t.

  In principle, a hash value could thought of as just an unsigned number
  without size semantics (uint32_t or uint64_t).  But it is used as
  index at some places, and so, size_t is also eligible. Therea re some
  places where assignments occur between hash_T and size_t variables, in
  both directions. Therefore, if we define hash_T to be of a type having
  a different width than that of size_t, we will have an incorrect
  assignment somewhere that will require an assert/guard.  So the most
  sensible option here seems to do hast_T to be size_t too.

- hashtab_T.ht_mask: long_u -> hash_T.

  Masks are used to be combined with hash_T values, so they should be of
  the same type.

hashtab.c:
- hash_may_resize(): oldsize: long_u -> size_t.
- hash_may_resize(): newsize: long_u -> size_t.
- hash_may_resize(): newmask: long_u -> hash_T.
2014-05-24 01:17:47 +02:00
Eliseo Martínez
0c68623aca Remove long_u: hashtab: Enable -Wconversion.
- Add hashtab.c to converted files list.
- Fix conversion issues:
    * hash_lookup()     : idx      : unsigned -> hash_T.
    * hash_may_resize() : minitems : int      -> size_t.
    * hash_may_resize() : newi     : unsigned -> hash_T.
    * hash_may_resize() : minsize  : long_u   -> size_t.
2014-05-24 01:17:46 +02:00
Eliseo Martínez
4d97ae66f9 Remove long_u: hashtab: Cleanup: Others.
hashtab.h:
- Add missing includes.
- Move hash_T to the top and use it to define hashtab_T.
- Move hash_removed related definitions to the top, as they are used in
  the definition of hashtab_T.
- Reformat multiline expression (start continuation with operator).
- Reformat function declaration into one single line.

hashtab.c:
- Use C99 style variable declarations (move declarations as near to
  first-usage point as possible).
- Simplify oldarray/newarray computation.
- Simplify unneeded else branch.
- Remove redundant casts.
2014-05-24 01:17:45 +02:00
Eliseo Martínez
98255c7a78 Remove long_u: hashtab: Cleanup: Comments.
- Restyle comments (/// when appropiate, // otherwise).
- Improve comments (add new comments, augment/clarify existing ones).
2014-05-24 01:17:43 +02:00
Eliseo Martínez
95c3300ca6 Remove long_u: do_outofmem_msg().
Remove long_u occurrences due to do_outofmem_msg() function.
Refactor size parameter from long_u into size_t.
2014-05-24 01:17:42 +02:00
Thiago de Arruda
9e95c8aa33 Merge branch 'use-uids-for-api-remote-objects' 2014-05-23 18:10:52 -03:00
Thiago de Arruda
6c96e42e2c API: Test: Setup basic test infrastructure
- Add a 'expect' utility script that can run simple API tests using clients
  developed for any platform.
- Extend travis build matrix to run API tests using the python client and
  valgrind.

This script can be used to write API tests without having to manage nvim's
lifetime:

- It starts a single nvim instance listening on a known socket
- Invokes the test runner, which should connect to NEOVIM_LISTEN_ADDRESS
- The nvim instance started by the script provides a `BeforeEachTest` function,
  which should be called before each test to reset nvim to a clean state.
- It takes care of shutting down nvim once the tests are finished.

As explained
[here](https://github.com/neovim/neovim/pull/737#issuecomment-43941520), it's
not possible to fully reset nvim to it's initial state, but the `BeforeEachTest`
function should be enough for most test cases. Tests requiring a fully clean
nvim instance should take care of starting/stopping nvim.
2014-05-23 16:06:59 -03:00
Thiago de Arruda
f03a7672e1 API: Refactor: Fix buffer_get_mark 2014-05-23 16:06:59 -03:00
Thiago de Arruda
9815688fbd API: Refactor: Use macro for initializing all arrays 2014-05-23 16:06:59 -03:00
Thiago de Arruda
1156360fe7 API: Refactor: Implement buffer_get_number 2014-05-23 16:06:59 -03:00
Thiago de Arruda
a842fe4dc1 API: Refactor: Return handles instead of indexes
- Define specialized arrays for each remote object type
- Implement msgpack_rpc functions for dealing with the new types
- Refactor all functions dealing with buffers, windows and tabpages to
  return/accept handles instead of list indexes.
2014-05-23 16:06:58 -03:00
Thiago de Arruda
f70f9bfac1 API: Refactor: Change the integer type of remote objects to uint64_t 2014-05-23 16:06:58 -03:00
Thiago de Arruda
1e67b13fdc API: Refactor: Add macro infrastructure for typed arrays
- Add macros supporting typed arrays in the remote API
- Refactor StringArray-related functions on top of the new macros
2014-05-23 16:06:58 -03:00
Thiago de Arruda
92307201b5 API: Refactor: Generalize buffer, window and tabpage types/functions
- Extract remote types definitions into a macro
- Extract msgpack_rpc helper functions for remote types into a macro
2014-05-23 16:06:58 -03:00
Thiago de Arruda
5fdf854f78 API: Refactor: Register/unregister created/destroyed tabpages
- Add the 'handle' field to `tabpage_T`
- Add declare/implement functions for registering/unregistering/retrieving
  tabpages
- Register/unregister tabpages when they are created/destroyed.
2014-05-23 16:06:58 -03:00
Thiago de Arruda
20848c4064 API: Refactor: Register/unregister created/destroyed windows
- Add the 'handle' field to `win_T`
- Add declare/implement functions for registering/unregistering/retrieving
  windows
- Register/unregister windows when they are created/destroyed.
2014-05-23 16:06:58 -03:00
Thiago de Arruda
ed99198ff1 API: Refactor: Register/unregister created/destroyed buffers
- Add the 'handle' field to `buf_T`
- Add declare/implement functions for registering/unregistering/retrieving
  buffers
- Register/unregister buffers when they are created/destroyed.
2014-05-23 16:06:58 -03:00
Thiago de Arruda
7dfc7bc2e1 API: Refactor: Implement api/handle module
This module will be used to implement remote management of objects through the
API. Object types to be registered must have a `uint64_t` field named 'handle'.
2014-05-23 16:06:58 -03:00
Thiago de Arruda
72e3125f45 API: Refactor: Move non-public files to private subdirectory 2014-05-23 16:06:58 -03:00
Thiago de Arruda
399a0e3740 API: Bugfix: Terminate directory string in vim_change_directory
Also check that the string length is not equal or greater than MAXPATHL.
2014-05-23 16:06:46 -03:00
Thiago de Arruda
677d30d796 API: Bugfix: Use 0-terminated string in vim_strwidth
While the mb_string2cells function accepts a length parameter, it only seems to
work properly with 0-terminated strings, since valgrind reports a conditional
jump that depends on uninitialized values(means it reads after the string
boundaries which could result in overflows or wrong results)
2014-05-23 15:49:19 -03:00
Thiago de Arruda
c6483aa2fa API: Bugfix: Fix loop condition in vim_list_runtime_paths 2014-05-23 15:49:17 -03:00
Thiago de Arruda
ee60683b9a API: Bugfix: Remove wrong increment statement from buffer_set_slice 2014-05-23 15:49:14 -03:00
Thiago de Arruda
28eb3796b9 API: Bugfix: Check that error isn't set in buffer_get_line 2014-05-23 15:49:12 -03:00
Thiago de Arruda
7ce2d63fef API: Cleanup: Remove unnecessary NULL checks 2014-05-23 15:49:08 -03:00
Justin M. Keyes
f1e52c496d Merge #739 'Remove OOM error handling in khash.h' 2014-05-22 17:00:55 -04:00
Justin M. Keyes
e2e47803bd Merge #708 'Remove NULL/non-NULL tests after vim_str(n)save'
- replace alloc with xmalloc
2014-05-22 13:00:51 -04:00
Pavel Platto
c57c3633d4 Remove OOM error handling in khash.h 2014-05-20 16:50:36 +03:00
Thiago de Arruda
0aa8b5828c Merge pull request #699 'Remove cryptography' 2014-05-20 08:31:19 -03:00
John Schmidt
85338fe1d5 Remove cryptography
As discussed in #694, vim encryption uses old,
obsolete algorithms that are poorly implemented.
Since insecure cryptography is worse than no
cryptgraphy, the community voted in favor of
removing all crypto.

Various alternatives to the old crypto is
being discussed in #701.

Closes #694.
2014-05-20 08:31:06 -03:00
Felipe Oliveira Carvalho
e303a11ebf Remove OOM checks: suggested changes in review
- Replace a vim_strsave/free pair with xrealloc
 - Use xmallocz() in some places
 - Use xrealloc() and forget about the NULL pointer case
 - Remove invalid comment
 - Remove unnecessary checks
 - Replace a complicated xmalloc/STRCPY/free code chunk code with xrealloc()
 - Replace a vim_strsave/free code chunk with xrealloc()
2014-05-19 14:50:26 -03:00
Felipe Oliveira Carvalho
7a830d945f Remove OOM checks: viminfo_filename() 2014-05-19 14:50:26 -03:00
Felipe Oliveira Carvalho
f88a2d7e1b Remove OOM checks: do_string_sub() 2014-05-19 14:50:25 -03:00
Felipe Oliveira Carvalho
1d844dda46 Remove OOM checks: expand_tag_fname() and tag_full_name() 2014-05-19 14:50:25 -03:00
Felipe Oliveira Carvalho
6ea2559f6c Remove OOM checks: u_save_line() 2014-05-19 14:50:25 -03:00
Felipe Oliveira Carvalho
1a2364f74e Remove OOM checks: ff_create_stack_element() 2014-05-19 14:50:25 -03:00
Felipe Oliveira Carvalho
8551f4f4c7 Remove OOM checks: mark_line() 2014-05-19 14:50:25 -03:00
Felipe Oliveira Carvalho
bf3d093627 truncate_line() cant't FAIL: change its return type to void 2014-05-19 14:50:25 -03:00
Felipe Oliveira Carvalho
11cae8ec58 Remove OOM checks: backslash_halve_save() 2014-05-19 14:50:25 -03:00
Felipe Oliveira Carvalho
a2f6a53b68 Remove OOM checks: save_typebuf() 2014-05-19 14:50:24 -03:00
Felipe Oliveira Carvalho
39a272c4db Remove OOM checks: alloc_typebuf() 2014-05-19 14:50:24 -03:00