Commit graph

122 commits

Author SHA1 Message Date
Thomas Haller
ea3b979a42
shared: add static asserts for size/alignment of NMEtherAddr 2020-11-19 20:12:26 +01:00
Thomas Haller
a3aa3725e5
shared,all: cleanup nm_utils_hashtable_equal*() functions
We have:

- nm_utils_hashtable_cmp(): this does a full cmp of two hash
  tables, with the intent to provide a stable sort order.
  It thus takes a GCompareDataFunc() argument.

- nm_utils_hashtable_cmp_equal(): this is like nm_utils_hashtable_cmp(),
  except that the caller won't get a compare value, only a boolean
  value that indicates equality.
  This was previously called nm_utils_hashtable_equal().

- nm_utils_hashtable_equal(): this takes a GEqualFunc function
  for comparing the values for equality. It takes thus
  a different kind of predicate, but otherwise is similar to
  nm_utils_hashtable_cmp_equal().
  This was previously called nm_utils_hash_table_equal().

Unify the naming of these functions.
2020-11-09 17:25:25 +01:00
Thomas Haller
d52c3b3c94
shared: use GEqualFunc instead of NMUtilsHashTableEqualFunc typedef 2020-11-09 17:25:25 +01:00
Thomas Haller
0cf4250021
shared: add nm_utils_strdict_clone() helper 2020-11-09 17:25:24 +01:00
Thomas Haller
571aeec933
shared: add nm_utils_named_value_clear_with_g_free() helper 2020-11-09 17:25:24 +01:00
Thomas Haller
6e44842dc6
shared: introduce opaque type NMUtilsUserData for nm_utils_user_data_pack()
This makes the usage slightly more type safe.
2020-10-22 17:38:08 +02:00
Thomas Haller
251ba8ea44
shared: automatically cast strv argument for nm_utils_strv_equal()/nm_utils_strv_cmp_n()
It's cumbersome if we always need to cast our arguments for
the strv helper functions. Depending on the situation, we often
have a "char **" or a "const char *const*" argument.

Use NM_CAST_STRV_CC() macros instead. This macro uses C11's _Generic()
and casts types that are presumed to be safe. This tends to be less
typing and more type-safe, because you don't need an explicit C cast
(which would overrule any warning that the compiler might have for you).
2020-10-12 22:20:01 +02:00
Thomas Haller
cbcfc58794
all/trivial: rename _nm_utils_strv_equal()/_nm_utils_strv_cmp_n() to have no leading underscore
The underscore somehow indicated that these would be an internal
function. Which they are in the sense that they are in "shared/nm-glib-aux/".
But they part of our internal helper functions, and in our code base
their use is no discouraged or "private.

Also, next I'll replace the function call with a macro, so, I will
have a need for the underscore name.

Rename.
2020-10-12 22:18:26 +02:00
Thomas Haller
88071abb43
all: unify comment style for SPDX-License-Identifier tag
Our coding style recommends C style comments (/* */) instead of C++
(//). Also, systemd (which we partly fork) uses C style comments for
the SPDX-License-Identifier.

Unify the style.

  $ sed -i '1 s#// SPDX-License-Identifier: \([^ ]\+\)$#/* SPDX-License-Identifier: \1 */#' -- $(git ls-files -- '*.[hc]' '*.[hc]pp')
2020-09-29 16:50:53 +02:00
Thomas Haller
8841d529e1
format: manually replace remaining tabs with spaces and reformat 2020-09-29 09:12:27 +02:00
Thomas Haller
740b092fda
format: replace tabs for indentation in code comments
sed -i \
     -e 's/^'$'\t'' \*/     */g' \
     -e 's/^'$'\t\t'' \*/         */g' \
     -e 's/^'$'\t\t\t'' \*/             */g' \
     -e 's/^'$'\t\t\t\t'' \*/                 */g' \
     -e 's/^'$'\t\t\t\t\t'' \*/                     */g' \
     -e 's/^'$'\t\t\t\t\t\t'' \*/                         */g' \
     -e 's/^'$'\t\t\t\t\t\t\t'' \*/                             */g' \
     $(git ls-files -- '*.[hc]')
2020-09-28 16:07:52 +02:00
Antonio Cardace
328fb90f3e
all: reformat all with new clang-format style
Run:

    ./contrib/scripts/nm-code-format.sh -i
    ./contrib/scripts/nm-code-format.sh -i

Yes, it needs to run twice because the first run doesn't yet produce the
final result.

Signed-off-by: Antonio Cardace <acardace@redhat.com>
2020-09-28 16:07:51 +02:00
Thomas Haller
39b52d0fd0
shared: add nm_utils_uid_to_name()/nm_utils_name_to_uid() helpers
These are inspired by systemd.

We should replace our calls to getpwuid() and getpwnam() with
their thread safe variants.

We run possibly multiple threads (e.g. helper threads from GDBus and
GResolver). It's hard to be sure that they don't also access the
functions.
2020-09-25 21:03:27 +02:00
Thomas Haller
3aca45ed55
shared,libnm: avoid mixing signed and unsigned integers in netmask_to_prefix()/prefix_to_netmask() 2020-09-24 11:07:36 +02:00
Thomas Haller
dc3a477884
all/style: remove duplicate semicolon (";;") in sources 2020-09-23 12:43:47 +02:00
Thomas Haller
b8811d97a4
all: require a semicolon after NM_CACHED_QUARK_FCN() 2020-09-23 10:55:17 +02:00
Thomas Haller
e8dd19bb01
shared: extend nm_utils_hexstr2bin_full() to require hexdigits in pairs
nm_utils_hexstr2bin_full() is our general hexstr to binary parsing
method. It uses (either mandatory or optional) delimiters. Before,
if delimiters are in use, it would accept individual hexdigits.
E.g. "a:b" would be accepted as "0a:0b:.

Add an argument that prevents accepting such single digits.
2020-09-22 17:40:41 +02:00
Thomas Haller
479de883b3
shared: add m_g_variant_singleton_u_0()
We anyway cache our variants for the properties of NMDBusObject instances.
If such a variant is well known to be always the same, there is no need
to allocate a new instance every time. In particular, because GVariant
is an immutable and ref counted type.

Add a singleton getter for a "u" variant with numeric value 0.
2020-09-18 15:26:37 +02:00
Thomas Haller
348ab39f6d
shared: add nm_utils_hashtable_{equal,cmp}() helper function 2020-09-11 10:45:40 +02:00
Thomas Haller
265a5879ae
shared: add nm_g_timeout_source_new_seconds() 2020-09-03 11:39:39 +02:00
Thomas Haller
9b54d175bf
shared: avoid multiple strlen() calls in _nm_utils_strv_dup_packed()
Instead of calling strlen() for all strings twice, remember the length
in a temporary buffer.

Yes, this is measurably faster.
2020-08-25 12:45:26 +02:00
Thomas Haller
2e0cd52474
shared: add nm_utils_strv_dup_packed() util 2020-08-25 09:54:11 +02:00
Thomas Haller
c25f4d947a
shared: enforce compatible C-type argument for nm_utils_strv_dup()
Use a macro that uses NM_CAST_STRV_CC() to cast the strv argument. Note that
NM_CAST_STRV_CC() uses C11's _Generic() to check whether the argument is
of a valid type.
2020-08-25 08:54:36 +02:00
Thomas Haller
7fbfe4381e
shared/trivial: add gtk-doc for nm_utils_buf_utf8safe_unescape() 2020-08-24 15:19:12 +02:00
Thomas Haller
70971d1141
all: avoid wrong compiler warning about uninitalized variables with LTO
Seems with LTO the compiler can sometimes think that thes variables are
uninitialized. Usually those code paths are only after an assertion was
hit (g_return*()), but we still need to workaround the warning.
2020-08-17 15:18:02 +02:00
Thomas Haller
07264a5dc1
shared: avoid "-Wmaybe-uninitialized" in _char_lookup_has()
shared/nm-glib-aux/nm-shared-utils.c: In function ‘nm_utils_escaped_tokens_escape_full’:
   shared/nm-glib-aux/nm-shared-utils.c:1569:26: error: ‘ch_lookup_as_needed.<U7d10>.table[0]’ may be used uninitialized in this function [-Werror=maybe-uninitialized]
    1569 |  nm_assert (lookup->table[(guint8) 0] == 0);
         |                          ^
   shared/nm-glib-aux/nm-shared-utils.c:1569:26: error: ‘ch_lookup_as_needed.<U7d10>.table[0]’ may be used uninitialized in this function [-Werror=maybe-uninitialized]

Fixes: ae55b98e02 ('shared: add CharLookupTable type for nm_utils_strsplit_set_full()')
2020-08-07 11:38:50 +02:00
Thomas Haller
ae55b98e02
shared: add CharLookupTable type for nm_utils_strsplit_set_full()
It's not a large difference, whether we use a 256 array directly or a
struct. I guess, theoretically this gives the compiler some more
information about the type, like the alignment. Also, it seems nicer
to use a dedicated struct instead of a pointer array.
2020-08-06 18:12:01 +02:00
Thomas Haller
5da502a1e1
shared,core: extend nm_utils_is_specific_hostname() and move to shared
nm_utils_is_specific_hostname() is basically to check whether the
hostname is localhost (and also handle "(null)").

In that sense, it's similar to systemd's is_localhost(). Extend or
variant to

  - be case insensitive (like is_localhost()).

  - accept more variants of localhost/localdomain names as special.
2020-08-05 12:47:55 +02:00
Thomas Haller
00c6823ecc
shared: add nm_strcmp_ascii_case_with_data() helper 2020-08-05 10:22:11 +02:00
Thomas Haller
66d4af6daf
shared: add compat function nm_g_ptr_array_copy() for older glib
This is not in "nm-glib.h", because it's not a complete replacement.
In glib before 2.62, it's not possible to implement g_ptr_array_copy()
as glib provides it, because the element_free_func is not accessible.

So, instead add our own implemented, which follows glib's version as
much as it can.
2020-07-24 16:03:15 +02:00
Thomas Haller
1e870c675d
shared: avoid heap allocation for temporary string in nm_utils_parse_inaddr_prefix_bin() 2020-07-23 15:29:22 +02:00
Thomas Haller
f78e0bf246
shared: assert that nm_utils_strsplit_set_full() returns non-empty strv array 2020-07-10 13:12:40 +02:00
Beniamino Galvani
4599be093e libnm-core: don't print value for no-value attributes 2020-07-08 09:43:06 +02:00
Beniamino Galvani
ee946ca27d libnm-core: pass variant-attribute-spec to format function
The output of nm_utils_format_variant_attributes() must be accepted by
nm_utils_parse_variant_attributes(), producing the initial attributes.

The latter has a special handling of some attributes, depending on the
input NMVariantAttributeSpec list. For example, if the
NMVariantAttributeSpec is a boolean with the 'no_value' flag, the
parser doesn't look for a value.

Pass the NMVariantAttributeSpec list to the format function so that it
can behave in the same way as the parse one.
2020-07-08 09:43:06 +02:00
Yuri Chornoivan
4e33f8cd89
all: fix minor typos
https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/565
2020-07-07 11:33:46 +02:00
Thomas Haller
220825836a
shared: move nm_utils_get_next_realloc_size() to nm-std-aux 2020-07-06 14:11:22 +02:00
Sayed Shah
7337ab8959
all: fix typo in man pages
There should be a comma after 'Otherwise' and 'Currently'.

https://bugzilla.redhat.com/show_bug.cgi?id=1852452

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/560
2020-07-03 10:48:04 +02:00
Thomas Haller
e2bd722358
shared: refactor nm_utils_parse_next_line() and add tests
- add unit test for nm_utils_parse_next_line()

- as line delimiter also accept "\r\n" and "\r" (beside "\n", "\0" and
  EOF).

- fix returning lines with embedded "\0" characters. The line ends
  on the first "\n" or "\0", whatever comes first. The code before
  didn't ensure that with:

     line_end = memchr (line_start, '\n', *inout_len);
     if (!line_end)
         line_end = memchr (line_start, '\0', *inout_len);
2020-07-03 10:34:27 +02:00
Antonio Cardace
aa5959a595
nm-shared-utils: add util to parse out lines from a string
https://bugzilla.redhat.com/show_bug.cgi?id=1821787
2020-06-26 16:47:56 +02:00
Thomas Haller
069be33fbd
shared: add nm_hexchar() helper 2020-06-26 09:29:52 +02:00
Thomas Haller
081650eb67
shared: avoid copying empty string in nm_str_buf_append_printf() 2020-06-25 22:46:27 +02:00
Thomas Haller
10779d545a
shared: add nm_utils_strsplit_quoted()
We want to parse "/proc/cmdline". That is space separated with support
for quoting and escaping. Our implementation becomes part of stable
behavior, and we should interpret the kernel command line the same way
as the system does. That means, our implementation should match
systemd's.
2020-06-23 00:42:37 +02:00
Thomas Haller
fd34fe50a2
shared: fix string truncation in nm_str_buf_append_printf()
If g_vsnprintf() returns that it wants to write 5 characters, it
really needs space for 5+1 characters. If we have 5 characters
available, it would have written "0123\0", which leaves the buffer
broken.

Fixes: eda47170ed ('shared: add NMStrBuf util')
2020-06-21 12:41:53 +02:00
Thomas Haller
83c79bc7a8
shared: allow empty NMStrBuf buffers with un-allocated memory
Previously, for simplicity, NMStrBuf did not support buffers without any
data allocated. However, supporting that has very little
overhead/complexity, so do it.

Now you can initialize buffers to have no data allocated, and when
appending data, it will automatically grow.
2020-06-20 19:22:04 +02:00
Thomas Haller
fff812e255
shared: refactor nm_utils_strdict_to_variant_ass() to use nm_utils_named_values_from_strdict()
It is pretty much the same code this way, but shorter.
2020-06-19 17:07:26 +02:00
Thomas Haller
cdb38df7e5
shared,core: rename and move function to nm_utils_strdict_to_variant_asv() 2020-06-19 17:07:26 +02:00
Thomas Haller
ee9e1ceefc
shared: avoid allocating temporary buffer for nm_utils_named_values_from_strdict()
Iterating hash tables gives an undefined order. Often we want to have
a stable order, for example when printing the content of a hash or
when converting it to a "a{sv}" variant.

How to achieve that best? I think we should only iterate the hash once,
and not require additional lookups. nm_utils_named_values_from_strdict()
achieves that by returning the key and the value together. Also, often
we only need the list for a short time, so we can avoid heap allocating
the list, if it is short enough. This works by allowing the caller to
provide a pre-allocated buffer (usually on the stack) and only as fallback
allocate a new list.
2020-06-19 17:07:25 +02:00
Thomas Haller
393bc8c8f6
shared: add nm_utils_buf_utf8safe_escape_cp() helper 2020-06-11 16:49:26 +02:00
Thomas Haller
39127758bd
shared: add nm_utils_ether_addr_equal(), nm_utils_ether_addr_cmp() 2020-06-11 12:00:52 +02:00
Beniamino Galvani
f695dd8de3 libnm-core: support variant attributes of type int32 and uint64 2020-06-08 15:31:41 +02:00