Loading ...
Sorry, an error occurred while loading the content.

Bug in meAtoi -- leading zeros

Expand Messages
  • Thomas Hundt
    From what I can see there is a bug in the atoi routine used. I first noticed this when I was trying to remove leading zeroes from part of the ascii-time
    Message 1 of 4 , Dec 19, 2005
    • 0 Attachment
      From what I can see there is a bug in the atoi routine used.

      I first noticed this when I was trying to remove leading zeroes from
      part of the ascii-time variable:

      define-macro test
      set-variable #l1 "09"
      insert-string #l1
      insert-string " -> "
      set-variable #l1 &add #l1 0 ; "09" -> "9"
      insert-string #l1
      newline
      !emacro

      The output of this is
      09 -> 0
      when it should really be
      9 -> 9

      If I change the set-variable to "9", it comes out
      9 -> 9

      I tracked down the function, it ends up being in eval.c
      case UFADD: return meItoa(meAtoi(arg1) + meAtoi(arg2));

      in eextrn.h it is defined
      #define meAtoi(s) strtol((char *)(s),(char **)NULL,0)
      and the 0 here is the base, and is a special value meaning, interpret
      strings beginning with 0x as hex, and strings beginning with 0 as octal.
      Which is why my "09" comes out as zero: 9 is undefined in octal.

      Sure enough, if I change my input string to "07", I get
      07 -> 7

      So, my workaround is going to be to prepend a "1" to make it a decimal
      1xx number and then subtract off the 100:

      set-variable #l1 &sub &cat "1" #l1 100 ; "09" -> "9"

      What I really want to do is fix the base to be, e.g., 10, but would that
      break other things? I don't know.
    • Thomas Hundt
      From what I can see there is a bug in the atoi routine used. I first noticed this when I was trying to remove leading zeroes from part of the ascii-time
      Message 2 of 4 , Dec 19, 2005
      • 0 Attachment
        From what I can see there is a bug in the atoi routine used.

        I first noticed this when I was trying to remove leading zeroes from
        part of the ascii-time variable:

        define-macro test
        set-variable #l1 "09"
        insert-string #l1
        insert-string " -> "
        set-variable #l1 &add #l1 0 ; "09" -> "9"
        insert-string #l1
        newline
        !emacro

        The output of this is
        09 -> 0
        when it should really be
        9 -> 9

        If I change the set-variable to "9", it comes out
        9 -> 9

        I tracked down the function, it ends up being in eval.c
        case UFADD: return meItoa(meAtoi(arg1) + meAtoi(arg2));

        in eextrn.h it is defined
        #define meAtoi(s) strtol((char *)(s),(char **)NULL,0)
        and the 0 here is the base, and is a special value meaning, interpret
        strings beginning with 0x as hex, and strings beginning with 0 as octal.
        Which is why my "09" comes out as zero: 9 is undefined in octal.

        Sure enough, if I change my input string to "07", I get
        07 -> 7

        So, my workaround is going to be to prepend a "1" to make it a decimal
        1xx number and then subtract off the 100:

        set-variable #l1 &sub &cat "1" #l1 100 ; "09" -> "9"

        What I really want to do is fix the base to be, e.g., 10, but would that
        break other things? I don't know.
      • Phillips, Steven
        Changing meAtoi to not handle C style base numeric notation would most definitely break a lot of things - see me.emf or grep for 0x in the macros directory.
        Message 3 of 4 , Dec 20, 2005
        • 0 Attachment
          Changing meAtoi to not handle C style base numeric notation would most
          definitely break a lot of things - see me.emf or grep for '0x' in the
          macros directory.

          The hex notation is used a lot as any numbers used for a bit based flag
          are far more readable in hex. The octal form is certainly used very
          rarely (the only place I can think of where it is used is with routines
          than manipulate the $buffer-fmod variable on unix) so removing support
          for octal would have less impact, but by using strtol ME is using a
          standard numerical form understood by any C/C++ programmer. For this
          reason I am reluctant to change it.

          Steve

          > -----Original Message-----
          > From: jasspa@yahoogroups.com [mailto:jasspa@yahoogroups.com] On Behalf
          Of
          > Thomas Hundt
          > Sent: Monday, December 19, 2005 7:18 PM
          > To: JASSPA MicroEmacs Mailing List (W)
          > Subject: [jasspa] Bug in meAtoi -- leading zeros
          >
          > From what I can see there is a bug in the atoi routine used.
          >
          > I first noticed this when I was trying to remove leading zeroes from
          > part of the ascii-time variable:
          >
          > define-macro test
          > set-variable #l1 "09"
          > insert-string #l1
          > insert-string " -> "
          > set-variable #l1 &add #l1 0 ; "09" -> "9"
          > insert-string #l1
          > newline
          > !emacro
          >
          > The output of this is
          > 09 -> 0
          > when it should really be
          > 9 -> 9
          >
          > If I change the set-variable to "9", it comes out
          > 9 -> 9
          >
          > I tracked down the function, it ends up being in eval.c
          > case UFADD: return meItoa(meAtoi(arg1) + meAtoi(arg2));
          >
          > in eextrn.h it is defined
          > #define meAtoi(s) strtol((char *)(s),(char **)NULL,0)
          > and the 0 here is the base, and is a special value meaning, interpret
          > strings beginning with 0x as hex, and strings beginning with 0 as
          octal.
          > Which is why my "09" comes out as zero: 9 is undefined in octal.
          >
          > Sure enough, if I change my input string to "07", I get
          > 07 -> 7
          >
          > So, my workaround is going to be to prepend a "1" to make it a decimal
          > 1xx number and then subtract off the 100:
          >
          > set-variable #l1 &sub &cat "1" #l1 100 ; "09" -> "9"
          >
          > What I really want to do is fix the base to be, e.g., 10, but would
          that
          > break other things? I don't know.
          >
          >
          >
          >
          > ------------------------ Yahoo! Groups Sponsor
          >
          >
          ________________________________________________________________________
          __
          >
          > This is an unmoderated list. JASSPA is not responsible for the content
          of
          > any material posted to this list.
          >
          > To unsubscribe, send a mail message to
          >
          > mailto:jasspa-unsubscribe@yahoogroups.com
          >
          > or visit http://groups.yahoo.com/group/jasspa and
          > modify your account settings manually.
          >
          >
          >
          > Yahoo! Groups Links
          >
          >
          >
          >
        • Thomas Hundt
          Yeah I guess it *is* documented -- on the help page macroOverview(4), MACRO LANGUAGE OVERVIEW : All variables are treated as strings within MicroEmacs so the
          Message 4 of 4 , Dec 20, 2005
          • 0 Attachment
            Yeah I guess it *is* documented -- on the help page macroOverview(4),
            "MACRO LANGUAGE OVERVIEW":

            All variables are treated as strings within MicroEmacs so the integer
            value of 102 is the string "102". Where functions require an integer
            argument (i.e. &add) the strings are converted to integers, operated on
            and converted back to a string result. Floating point notation is not
            understood.

            Integers may be expressed in different bases:

            0xXXX - Hexadecimal notation i.e. 0x12af.
            0oooo - Octal notation requires a leading 012673.
            ddddd - Decimal notation 12345.

            It just took me by surprise, I guess because other things (e.g., the Esc
            arg or universal-argument) are locked in as decimal.

            Thanks

            -Th



            Phillips, Steven wrote:
            > Changing meAtoi to not handle C style base numeric notation would
            > most definitely break a lot of things - see me.emf or grep for '0x'
            > in the macros directory.
            >
            > The hex notation is used a lot as any numbers used for a bit based
            > flag are far more readable in hex. The octal form is certainly used
            > very rarely (the only place I can think of where it is used is with
            > routines than manipulate the $buffer-fmod variable on unix) so
            > removing support for octal would have less impact, but by using
            > strtol ME is using a standard numerical form understood by any C/C++
            > programmer. For this reason I am reluctant to change it.
            >
            > Steve
            >
            >> -----Original Message----- From: jasspa@yahoogroups.com
            >> [mailto:jasspa@yahoogroups.com] On Behalf
            > Of
            >> Thomas Hundt Sent: Monday, December 19, 2005 7:18 PM To: JASSPA
            >> MicroEmacs Mailing List (W) Subject: [jasspa] Bug in meAtoi --
            >> leading zeros
            >>
            >> From what I can see there is a bug in the atoi routine used.
            >>
            >> I first noticed this when I was trying to remove leading zeroes
            >> from part of the ascii-time variable:
            >>
            >> define-macro test set-variable #l1 "09" insert-string #l1
            >> insert-string " -> " set-variable #l1 &add #l1 0 ; "09" -> "9"
            >> insert-string #l1 newline !emacro
            >>
            >> The output of this is 09 -> 0 when it should really be 9 -> 9
            >>
            >> If I change the set-variable to "9", it comes out 9 -> 9
            >>
            >> I tracked down the function, it ends up being in eval.c case UFADD:
            >> return meItoa(meAtoi(arg1) + meAtoi(arg2));
            >>
            >> in eextrn.h it is defined #define meAtoi(s) strtol((char
            >> *)(s),(char **)NULL,0) and the 0 here is the base, and is a special
            >> value meaning, interpret strings beginning with 0x as hex, and
            >> strings beginning with 0 as
            > octal.
            >> Which is why my "09" comes out as zero: 9 is undefined in octal.
            >>
            >> Sure enough, if I change my input string to "07", I get 07 -> 7
            >>
            >> So, my workaround is going to be to prepend a "1" to make it a
            >> decimal 1xx number and then subtract off the 100:
            >>
            >> set-variable #l1 &sub &cat "1" #l1 100 ; "09" -> "9"
            >>
            >> What I really want to do is fix the base to be, e.g., 10, but would
            >>
            > that
            >> break other things? I don't know.
          Your message has been successfully submitted and would be delivered to recipients shortly.