From c6656c156720e1176e81cff6062f0308cf3edb0b Mon Sep 17 00:00:00 2001 From: Daniel Markstedt Date: Sat, 26 Sep 2026 12:22:02 +0200 Subject: [PATCH] nbplkup: safely handle charset conversion failures diff --git a/bin/nbp/meson.build b/bin/nbp/meson.build index 1191b1143..3fbe6502a 100644 --- a/bin/nbp/meson.build +++ b/bin/nbp/meson.build @@ -6,7 +6,7 @@ endif executable( 'nbplkup', - 'nbplkup.c', + ['nbplkup.c', 'nbplkup_output.c'], include_directories: root_includes, link_with: libatalk, dependencies: npb_deps, diff --git a/bin/nbp/nbplkup.c b/bin/nbp/nbplkup.c index 5e7dc4e13..3d6df6410 100644 --- a/bin/nbp/nbplkup.c +++ b/bin/nbp/nbplkup.c @@ -39,6 +39,8 @@ #include #include +#include "nbplkup_output.h" + #define MACCHARSET "MAC_ROMAN" static char *Obj = "="; @@ -229,19 +231,21 @@ int main(int ac, char **av) } for (i = 0; i < c; i++) { - obj_len = convert_string_allocate(chMac, CH_UNIX, nn[i].nn_obj, - nn[i].nn_objlen, &obj); - type_len = convert_string_allocate(chMac, CH_UNIX, nn[i].nn_type, - nn[i].nn_typelen, &type); + obj_len = nbplkup_convert_field(chMac, nn[i].nn_obj, + nn[i].nn_objlen, &obj); if ((size_t)(-1) == obj_len) { - obj_len = nn[i].nn_objlen; - obj = strdup(nn[i].nn_obj); + perror("nbplkup_convert_field"); + exit(1); + } - if (obj == NULL) { - perror("strdup"); - exit(1); - } + type_len = nbplkup_convert_field(chMac, nn[i].nn_type, + nn[i].nn_typelen, &type); + + if ((size_t)(-1) == type_len) { + perror("nbplkup_convert_field"); + free(obj); + exit(1); } if (script_friendly_output) { diff --git a/bin/nbp/nbplkup_output.c b/bin/nbp/nbplkup_output.c new file mode 100644 index 000000000..532040c6b --- /dev/null +++ b/bin/nbp/nbplkup_output.c @@ -0,0 +1,61 @@ +/* + * NBP lookup status message output formatting routines + * + * Copyright (c) 2026 Daniel Markstedt + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif /* HAVE_CONFIG_H */ + +#include +#include +#include +#include + +#include + +#include "nbplkup_output.h" + +size_t nbplkup_convert_field(charset_t from, const char *src, size_t src_len, + char **dest) +{ + size_t dest_len; + + if (src == NULL || dest == NULL) { + errno = EINVAL; + return (size_t) -1; + } + + *dest = NULL; + dest_len = convert_string_allocate(from, CH_UNIX, src, src_len, dest); + + if (dest_len != (size_t) -1) { + return dest_len; + } + + if (src_len == SIZE_MAX) { + errno = EOVERFLOW; + return (size_t) -1; + } + + *dest = malloc(src_len + 1); + + if (*dest == NULL) { + return (size_t) -1; + } + + memcpy(*dest, src, src_len); + (*dest)[src_len] = '\0'; + return src_len; +} diff --git a/bin/nbp/nbplkup_output.h b/bin/nbp/nbplkup_output.h new file mode 100644 index 000000000..f80a8e09e --- /dev/null +++ b/bin/nbp/nbplkup_output.h @@ -0,0 +1,11 @@ +#ifndef NETATALK_NBPLKUP_OUTPUT_H +#define NETATALK_NBPLKUP_OUTPUT_H + +#include + +#include + +size_t nbplkup_convert_field(charset_t from, const char *src, size_t src_len, + char **dest); + +#endif /* NETATALK_NBPLKUP_OUTPUT_H */