Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Regular expression bug (v4.2.0 RC1)


  • Please log in to reply
4 replies to this topic

#1 Casper

Casper

    New Member

  • Members
  • Pip
  • 6 posts

Posted 08 October 2011 - 11:31 AM

Hi all,

I think I found a bug in the recently added Regex library. Let's say we want a specific string from an AT string:


Debug.GC(true); // Force garbage collect

var regex = new Regex("\\+CUSD: 2,\"(.*?)\",0$"); // Our regular expression
var match = regex.Match("+CUSD: 2,\"Lorem ipsum dolor sit amet...\",0"); // The AT string

if (match.Success)
{
    Debug.Print(match.Groups[1].Value); // Print the result of the match
}


Debug.Print will correctly print: Lorem ipsum dolor sit amet...

But now the AT string is larger, let's say:

var match = regex.Match("+CUSD: 2,\"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt...\",0");

When executed the output will say: "Failed allocation for 57 blocks, 684 bytes" and the string in match.Groups[1].Value is corrupted.

As you can see I force the garbage collector to run to ensure the full 40K RAM is available. Any ideas?

#2 Stefan W.

Stefan W.

    Advanced Member

  • Members
  • PipPipPip
  • 153 posts

Posted 10 October 2011 - 10:59 AM

Regexes can be really memory-heavy and slow, you just don't notice on desktop computers as much. If you anchor your regex and make the group atomic, it should work.
var regex = new Regex("^\\+CUSD: 2,\"(?>[^\"]*)\",0$"); // Our regular expression

I believe that no discovery of fact, however trivial, can be wholly useless to the race, and that no trumpeting of falsehood, however virtuous in intent, can be anything but vicious.
-- H.L. Mencken, "What I Believe"

#3 Stefan

Stefan

    Moderator

  • Members
  • PipPipPip
  • 1965 posts
  • LocationBreda, the Netherlands

Posted 10 October 2011 - 11:09 AM

I just tested this code to the 4.2RC2 emulator:
using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;
using System.Text.RegularExpressions;

namespace NetduinoApplication1
{
    public class Program
    {
        public static void Main()
        {
            Debug.GC(true); // Force garbage collect

            var regex = new Regex("\\+CUSD: 2,\"(.*?)\",0$"); // Our regular expression
            //var match = regex.Match("+CUSD: 2,\"Lorem ipsum dolor sit amet...\",0"); // The AT string
            var match = regex.Match("+CUSD: 2,\"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt...\",0");

            if (match.Success)
            {
                Debug.Print(match.Groups[1].Value); // Print the result of the match
            }
        }

    }
}
and it worked fine.
"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs

#4 Stefan W.

Stefan W.

    Advanced Member

  • Members
  • PipPipPip
  • 153 posts

Posted 10 October 2011 - 11:27 AM

Does the emulator have the memory constraints of a specific device?
I believe that no discovery of fact, however trivial, can be wholly useless to the race, and that no trumpeting of falsehood, however virtuous in intent, can be anything but vicious.
-- H.L. Mencken, "What I Believe"

#5 Stefan

Stefan

    Moderator

  • Members
  • PipPipPip
  • 1965 posts
  • LocationBreda, the Netherlands

Posted 10 October 2011 - 11:41 AM

Does the emulator have the memory constraints of a specific device?

I hope so :) I can't test in RC2 on a netduino yet, since there's no rc2 firmware at this moment though.
"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.