Fix order of execution not to misuse va_arg
authorLukáš Jiřiště <gymnazium.jiriste@gmail.com>
Sun, 17 Aug 2025 10:32:11 +0000 (12:32 +0200)
committerLukáš Jiřiště <gymnazium.jiriste@gmail.com>
Sun, 17 Aug 2025 10:38:03 +0000 (12:38 +0200)
The index should not even load at depth 0.

ft_parse/ft_get_child_deep.c

index d2adcbc6b340ab9a6562d605051396425a9b7114..e32d9ec6ca6f70b04db61643bfe99ecef7fe5744 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <ljiriste@student.42prague.com>   +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2025/08/13 21:01:34 by ljiriste          #+#    #+#             */
-/*   Updated: 2025/08/13 21:45:10 by ljiriste         ###   ########.fr       */
+/*   Updated: 2025/08/17 12:37:48 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -19,11 +19,11 @@ t_parse_tree_node   *ft_vget_child_deep(
 {
        int     index;
 
+       if (!node || depth == 0)
+               return (node);
        index = va_arg(*args, int);
        if (index < 0)
                return (NULL);
-       if (!node || depth == 0)
-               return (node);
        return (ft_vget_child_deep(ft_get_node_child(node, (size_t)index), depth - 1, args));
 }
 
@@ -45,11 +45,11 @@ const t_parse_tree_node     *ft_vcget_child_deep(
 {
        int     index;
 
+       if (!node || depth == 0)
+               return (node);
        index = va_arg(*args, int);
        if (index < 0)
                return (NULL);
-       if (!node || depth == 0)
-               return (node);
        return (ft_vcget_child_deep(ft_cget_node_child(node, (size_t)index), depth - 1, args));
 }