LCOV - code coverage report
Current view: top level - libs/rational/src - rational_report.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 30 30
Test Date: 2026-07-12 01:01:34 Functions: 100.0 % 2 2
Branches: 100.0 % 8 8

             Branch data     Line data    Source code
       1                 :             : #include "rational.h"
       2                 :             : #include <unistd.h>
       3                 :             : #include <errno.h>
       4                 :             : #include <stdarg.h>
       5                 :             : #include <stdio.h>
       6                 :             : #include <string.h>
       7                 :             : 
       8                 :             : /**
       9                 :             :  * @brief Safely prints an error message with system error description to stderr
      10                 :             :  *
      11                 :             :  * @param prefix Custom message to prepend to the error (must not be NULL)
      12                 :             :  * @param file Source file name where the function was called (must not be NULL)
      13                 :             :  * @param func Name of the calling function (must not be NULL)
      14                 :             :  * @return void
      15                 :             :  *
      16                 :             :  * @note This function doesn't allocate memory dynamically and is safe to use
      17                 :             :  *       in low-memory conditions
      18                 :             :  *
      19                 :             :  * @example
      20                 :             :  *    serp("Failed to allocate memory"); <- NO EOL (\n)!
      21                 :             :  */
      22                 :           1 : void SERP(
      23                 :             :         const char *prefix,
      24                 :             :         const char *file,
      25                 :             :         const char *func)
      26                 :             : {
      27                 :             :         /* Buffer for converting errno to string, size 32 is sufficient for max int */
      28                 :             :         char itoa_buf[32];
      29                 :             : 
      30                 :           1 :         fputs("ERROR: ",stderr);
      31                 :           1 :         fputs(prefix,stderr);
      32                 :           1 :         fputs(" [File: ",stderr);
      33                 :           1 :         fputs(file,stderr);
      34                 :           1 :         fputs(", Function: ",stderr);
      35                 :           1 :         fputs(func,stderr);
      36                 :           1 :         fputs("] Errno: (",stderr);
      37                 :           1 :         fputs(itoa(errno,itoa_buf,10),stderr);
      38                 :           1 :         fputs(") ",stderr);
      39                 :           1 :         fputs(strerror(errno),stderr);
      40                 :           1 :         fputs("\n",stderr);
      41                 :           1 : }
      42                 :             : 
      43                 :             : /**
      44                 :             :  * @brief Reports an error message with debug info to stderr
      45                 :             :  *
      46                 :             :  * This function generates a formatted error message with variable arguments and
      47                 :             :  * writes it directly to stderr without using dynamic memory allocation. It uses
      48                 :             :  * fixed-size stack buffers and writes to @c STDERR_FILENO so the error message
      49                 :             :  * can still be emitted when the heap has no spare bytes left. Includes source
      50                 :             :  * location information and system error details.
      51                 :             :  *
      52                 :             :  * @param source_file Name of the source file where error occurred (must not be NULL)
      53                 :             :  * @param func_name Name of the function where error occurred (must not be NULL)
      54                 :             :  * @param line_num Line number where error occurred (must be positive)
      55                 :             :  * @param format Printf-style format string for the error message (must not be NULL)
      56                 :             :  * @param ... Variable arguments matching format specifiers
      57                 :             :  * @return void
      58                 :             :  *
      59                 :             :  * @note Maximum message length is limited by MAX_CHARACTERS
      60                 :             :  * @note This function must not allocate heap memory because it is used on
      61                 :             :  *       low-memory error paths
      62                 :             :  *
      63                 :             :  * @example
      64                 :             :  *    REPORT(__FILE__, __func__, __LINE__, "Failed to allocate %d bytes", size);
      65                 :             :  */
      66                 :             : __attribute__((format(printf,4,5)))
      67                 :         105 : void REPORT(
      68                 :             :         const char *source_file,
      69                 :             :         const char *func_name,
      70                 :             :         int        line_num,
      71                 :             :         const char *format,
      72                 :             :         ...)
      73                 :             : {
      74                 :             :         /* Buffers for message construction, zero-initialized for safety */
      75                 :         105 :         char msg_buffer[MAX_CHARACTERS] = {0};    /* User message buffer */
      76                 :         105 :         char final_buffer[MAX_CHARACTERS] = {0};  /* Complete error message */
      77                 :             : 
      78                 :             :         va_list args;
      79                 :         105 :         va_start(args,format);
      80                 :         105 :         vsnprintf(msg_buffer,sizeof(msg_buffer),format,args);
      81                 :         105 :         va_end(args);
      82                 :             : 
      83                 :             :         /* Format complete error message with debug info and errno details */
      84                 :         210 :         ssize_t len = snprintf(final_buffer,
      85                 :             :                 sizeof(final_buffer),
      86                 :             :                 "ERROR: %s:%s:%d %s Errno: %s (errno: %d)\n",
      87                 :             :                 source_file,
      88                 :             :                 func_name,
      89                 :             :                 line_num,
      90                 :             :                 msg_buffer,
      91                 :         105 :                 strerror(errno),
      92                 :         105 :                 errno
      93                 :             :         );
      94                 :             : 
      95                 :             :         /* Write message to stderr, handling potential buffer size limitations */
      96         [ +  + ]:         105 :         if(len > 0)
      97                 :             :         {
      98                 :         104 :                 size_t write_size = sizeof(final_buffer);
      99                 :             : 
     100         [ +  + ]:         104 :                 if((size_t)len < sizeof(final_buffer))
     101                 :             :                 {
     102                 :         103 :                         write_size = (size_t)len;
     103                 :             :                 }
     104                 :             : 
     105                 :             :                 /* Attempt to write error message */
     106         [ +  + ]:         104 :                 if(write(STDERR_FILENO,final_buffer,write_size) < 0)
     107                 :             :                 {
     108                 :             :                         /* Fallback error message if primary write fails */
     109                 :           2 :                         const char fallback_msg[] = "ERROR: Failed to write error message\n";
     110                 :             : 
     111         [ +  + ]:           2 :                         if(write(STDERR_FILENO,fallback_msg,sizeof(fallback_msg) - 1) < 0)
     112                 :             :                         {
     113                 :           1 :                                 return;
     114                 :             :                         }
     115                 :             :                 }
     116                 :             :         }
     117                 :             : }
        

Generated by: LCOV version 2.0-1