Fix: ini parser: truncation of value name
[lttng-tools.git] / src / common / config / ini.c
index 7462aeb35d224a2e414dedbafd5918026e31113e..c8cf307aa82c1853f47abdc8eaca5e0bb8771608 100644 (file)
@@ -3,7 +3,9 @@
  *
  * The "inih" library is distributed under the New BSD license:
  *
- * Copyright (c) 2009, Brush Technology - All rights reserved.
+ * Copyright (C) 2009 Brush Technology - All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are met:
@@ -34,6 +36,7 @@
 #include <stdio.h>
 #include <ctype.h>
 #include <string.h>
+#include <common/common.h>
 
 #include "ini.h"
 
@@ -81,12 +84,13 @@ static char* find_char_or_comment(const char* s, char c)
 /* Version of strncpy that ensures dest (size bytes) is null-terminated. */
 static char* strncpy0(char* dest, const char* src, size_t size)
 {
-       strncpy(dest, src, size);
+       strncpy(dest, src, size - 1);
        dest[size - 1] = '\0';
        return dest;
 }
 
 /* See documentation in header file. */
+LTTNG_HIDDEN
 int ini_parse_file(FILE* file, ini_entry_handler handler, void* user)
 {
        /* Uses a fair bit of stack (use heap instead if you need to) */
@@ -106,7 +110,7 @@ int ini_parse_file(FILE* file, ini_entry_handler handler, void* user)
        int error = 0;
 
 #if !INI_USE_STACK
-       line = (char*)malloc(INI_MAX_LINE);
+       line = (char*)zmalloc(INI_MAX_LINE);
        if (!line) {
                return -2;
        }
@@ -178,6 +182,10 @@ int ini_parse_file(FILE* file, ini_entry_handler handler, void* user)
                                 * Valid name[=:]value pair found, call
                                 * handler
                                 */
+                               if (strlen(name) >= sizeof(prev_name)) {
+                                       /* Truncation occurs, report an error. */
+                                       error = lineno;
+                               }
                                strncpy0(prev_name, name, sizeof(prev_name));
                                if (handler(user, section, name, value) < 0 &&
                                        !error) {
@@ -199,6 +207,7 @@ int ini_parse_file(FILE* file, ini_entry_handler handler, void* user)
 }
 
 /* See documentation in header file. */
+LTTNG_HIDDEN
 int ini_parse(const char* filename, ini_entry_handler handler, void* user)
 {
        FILE* file;
This page took 0.024855 seconds and 4 git commands to generate.