Ad Widget

Collapse

sprintf function issue

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kailas.awchar
    Junior Member
    • Dec 2008
    • 9

    #1

    sprintf function issue

    Hi all,

    I want to add some sprintf statement in zabbix code but whenever i add it it gives error to "do not use sprintf insted use zbx_snprintf function".

    Can any one let me know why sprintf function is not supported in zabbix.

    Any kind of help is very much appreciated.

    Thanks in advance for your updates on this.


    Regards,
    Kailas
  • Calimero
    Senior Member
    • Nov 2006
    • 481

    #2
    Portability and security, I guess.

    As the define suggests, try zbx_snprintf and other functions provided by zabbix.

    See include/common.h and src/libs/zbxcommon/str.c. You'll find what you need.

    Comment

    • kailas.awchar
      Junior Member
      • Dec 2008
      • 9

      #3
      Hi Calimero,

      Thanks for your quick reply.

      I know that i need to use zbx_snprintf insted of sprintf function, i have commented #define line and working further as i badly need to use the sprintf function.

      Thanks once again.

      Kaials.

      Comment

      • alixen
        Senior Member
        • Apr 2006
        • 474

        #4
        You should really use zbx_snprintf because sprintf can lead to memory corruption and became a security hole.

        What are you doing with sprintf that cannot be done with zbx_snprintf ?



        Alixen
        http://www.alixen.fr/zabbix.html

        Comment

        • kailas.awchar
          Junior Member
          • Dec 2008
          • 9

          #5
          sprintf function issue

          Hi Alixen,

          Sorry for late reply..... I dont know much about zbx_snprintf can you please let me know what it does and how to use it.

          Actually i want to use snprintf to convert the string format into the hex with help of "%x". can it be possible with zbx_snprintf.

          I am using it as below:-
          snprintf(Variabl1,"%x",variable2);

          so that variable2 will get converted to hex format and assigned to variable1.

          Thanks in advance for your valuable help.

          Regards,
          Kailas Awchar
          Last edited by kailas.awchar; 10-08-2009, 12:00. Reason: Speling mistake.

          Comment

          • Calimero
            Senior Member
            • Nov 2006
            • 481

            #6
            zabbix_*printf functions are close copies (or thin layers above) the "original" *printf() functions. So you should be able to give them somehow the same arguments.

            snprint() is a close (and safer) friend of sprintf as you have to tell it the maximum size it can write. This will usually prevent overflows.

            Comment

            • alixen
              Senior Member
              • Apr 2006
              • 474

              #7
              Hi,

              Originally posted by kailas.awchar
              Sorry for late reply..... I dont know much about zbx_snprintf can you please let me know what it does and how to use it.
              As Calimero said, it is a thin layer around snprintf. If you need more information, it is included in Zabbix source; you can get first hand information by reading it.

              Originally posted by kailas.awchar
              Actually i want to use snprintf to convert the string format into the hex with help of "%x". can it be possible with zbx_snprintf.

              I am using it as below:-
              snprintf(Variabl1,"%x",variable2);

              so that variable2 will get converted to hex format and assigned to variable1.
              Are you sure of your syntax ?
              snprintf manual page defines it as:
              Code:
              int snprintf(char *str, size_t size, const char *format, ...);
              Hope this helps
              Alixen
              http://www.alixen.fr/zabbix.html

              Comment

              Working...